From e2721b1098905d502ff9bdb8fd014f12c890ce87 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Tue, 24 Sep 2024 21:17:02 -0400 Subject: [PATCH 1/7] adds week7 --- number-of-islands/yeonguchoe.cpp | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 number-of-islands/yeonguchoe.cpp diff --git a/number-of-islands/yeonguchoe.cpp b/number-of-islands/yeonguchoe.cpp new file mode 100644 index 000000000..108e1c926 --- /dev/null +++ b/number-of-islands/yeonguchoe.cpp @@ -0,0 +1,68 @@ +class Solution { +public: + int numIslands(vector>& grid) { + int count = 0; + + int row_size = grid.size(); + int column_size = grid[0].size(); + + queue> q; + + for (int i = 0; i < row_size; i++) { + for (int j = 0; j < column_size; j++) { + if (grid[i][j] == '1') { + LookAround(i, j, grid, q, row_size, column_size); + count += 1; + } + } + } + return count; + } + + // BFS + void LookAround(int& x, int& y, vector>& grid, + queue>& q, int& row_size, int& column_size) { + pair center_location{x, y}; + q.push(center_location); + + grid[x][y] = '0'; + + while (!q.empty()) { + pair selected_location = q.front(); + q.pop(); + + int selected_x = selected_location.first; + int selected_y = selected_location.second; + + // 선택된 cell에서 왼쪽 + if (selected_x - 1 >= 0 && + grid[selected_x - 1][selected_y] == '1') { + q.push({selected_x - 1, selected_y}); + grid[selected_x - 1][selected_y] = '0'; + } + + // 선택된 cell에서 오른쪽 + if (selected_x + 1 < row_size && + grid[selected_x + 1][selected_y] == '1') { + q.push({selected_x + 1, selected_y}); + grid[selected_x + 1][selected_y] = '0'; + } + + // 선택된 cell에서 위쪽 + if (selected_y - 1 >= 0 && + grid[selected_x][selected_y - 1] == '1') { + q.push({selected_x, selected_y - 1}); + grid[selected_x][selected_y - 1] = '0'; + } + + // 선택된 cell에서 아래쪽 + if (selected_y + 1 < column_size && + grid[selected_x][selected_y + 1] == '1') { + q.push({selected_x, selected_y + 1}); + grid[selected_x][selected_y + 1] = '0'; + } + } + } + // 시간 복잡도: O(row*column) + // 공간 복잡도: O(row*column) +}; From c90152892eb86f0777cf25271396e7dbb34dc764 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Tue, 24 Sep 2024 22:20:11 -0400 Subject: [PATCH 2/7] week7 --- set-matrix-zeroes/yeonguchoe.c | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 set-matrix-zeroes/yeonguchoe.c diff --git a/set-matrix-zeroes/yeonguchoe.c b/set-matrix-zeroes/yeonguchoe.c new file mode 100644 index 000000000..bea6246e8 --- /dev/null +++ b/set-matrix-zeroes/yeonguchoe.c @@ -0,0 +1,61 @@ +void setZeroes(int** matrix, int matrixSize, int* matrixColSize) { + + int row_size = matrixSize; + int column_size = matrixColSize[0]; + + bool zero_on_first_row = false; + bool zero_on_first_column = false; + + // 첫번째 row에 0이 존재하는지 확인 + for (int i = 0; i < column_size; i++) { + if (matrix[0][i] == 0) { + zero_on_first_row = true; + } + } + // 첫번째 column에 0이 존재하는지 확인 + for (int i = 0; i < row_size; i++) { + if (matrix[i][0] == 0) { + zero_on_first_column = true; + } + } + + // 전체 돌면서 0 발견시 첫번째 row, column에 0으로 표시 + for (int i = 0; i < row_size; i++) { + for (int j = 0; j < column_size; j++) { + if (matrix[i][j] == 0) { + matrix[0][j] = 0; + matrix[i][0] = 0; + } + } + } + + for (int i = 1; i < column_size; i++) { + if (matrix[0][i] == 0) { + for (int j = 1; j < row_size; j++) { + matrix[j][i] = 0; + } + } + } + + for (int i = 1; i < row_size; i++) { + if (matrix[i][0] == 0) { + for (int j = 1; j < column_size; j++) { + matrix[i][j] = 0; + } + } + } + + if (zero_on_first_row) { + for (int i = 0; i < column_size; i++) { + matrix[0][i] = 0; + } + } + + if (zero_on_first_column) { + for (int i = 0; i < row_size; i++) { + matrix[i][0] = 0; + } + } +} +// 시간 복잡도: O(matrix 크기) +// 공간 복잡도: O(1) From b664c6c2456ef3f22edaca8ae3fb50085ddf325a Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Tue, 24 Sep 2024 23:39:58 -0400 Subject: [PATCH 3/7] week 7 --- unique-paths/yeonguchoe.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 unique-paths/yeonguchoe.cpp diff --git a/unique-paths/yeonguchoe.cpp b/unique-paths/yeonguchoe.cpp new file mode 100644 index 000000000..28484874e --- /dev/null +++ b/unique-paths/yeonguchoe.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int combination(int n, int r) { + int k = n - r > r ? r : n - r; + unsigned long long result = 1; + for (int i = 0; i < k; i++) { + result = result * (n - i) / (i + 1); + } + return result; + } + int uniquePaths(int m, int n) { return combination(m - 1 + n - 1, m - 1); } + // 시간 복잡도: O(min(m,n)) + // 공간 복잡도: O(1) +}; From 2ef7791326962fad96201cf9ee1e2abb5910a808 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Wed, 25 Sep 2024 00:05:00 -0400 Subject: [PATCH 4/7] week7 --- reverse-linked-list/yeonguchoe.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse-linked-list/yeonguchoe.c diff --git a/reverse-linked-list/yeonguchoe.c b/reverse-linked-list/yeonguchoe.c new file mode 100644 index 000000000..def898296 --- /dev/null +++ b/reverse-linked-list/yeonguchoe.c @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* reverseList(struct ListNode* head) { + + struct ListNode* ptr1 = NULL; + struct ListNode* ptr2 = head; + struct ListNode* ptr3 = NULL; + + while (ptr2 != NULL) { + ptr3 = ptr2->next; + ptr2->next = ptr1; + + // 앞으로 전진 + ptr1 = ptr2; + ptr2 = ptr3; + } + return ptr1; +} +// 시간 복잡도: O(노드 개수) +// 공간 복잡도: O(1) From 9869f6b0733b3166ea9c35d14bbdce6961d8f0f1 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Wed, 25 Sep 2024 01:35:28 -0400 Subject: [PATCH 5/7] Create yeonguchoe.cpp --- .../yeonguchoe.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 longest-substring-without-repeating-characters/yeonguchoe.cpp diff --git a/longest-substring-without-repeating-characters/yeonguchoe.cpp b/longest-substring-without-repeating-characters/yeonguchoe.cpp new file mode 100644 index 000000000..1fe010fda --- /dev/null +++ b/longest-substring-without-repeating-characters/yeonguchoe.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + int current_max = 0; + int pivot = 0; + int checker = 0; + + int current_length = 0; + while (pivot < s.size()) { + set included; + included.insert(s[pivot]); + checker = pivot + 1; + while (checker < s.size() and + included.find(s[checker]) == included.end()) { + included.insert(s[checker]); + checker += 1; + } + current_length = checker - pivot; + current_max = max(current_max, current_length); + pivot += 1; + } + return current_max; + } + // 시간 복잡도: O(n) + // 공간 복잡도: O(n) +}; From fe8c66ab151624b3073c1b1433811dfdb84eed05 Mon Sep 17 00:00:00 2001 From: Yeongu Choe Date: Wed, 25 Sep 2024 01:55:31 -0400 Subject: [PATCH 6/7] adds week 7 --- .../yeonguchoe.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 longest-substring-without-repeating-characters/yeonguchoe.cpp diff --git a/longest-substring-without-repeating-characters/yeonguchoe.cpp b/longest-substring-without-repeating-characters/yeonguchoe.cpp new file mode 100644 index 000000000..1fe010fda --- /dev/null +++ b/longest-substring-without-repeating-characters/yeonguchoe.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + int current_max = 0; + int pivot = 0; + int checker = 0; + + int current_length = 0; + while (pivot < s.size()) { + set included; + included.insert(s[pivot]); + checker = pivot + 1; + while (checker < s.size() and + included.find(s[checker]) == included.end()) { + included.insert(s[checker]); + checker += 1; + } + current_length = checker - pivot; + current_max = max(current_max, current_length); + pivot += 1; + } + return current_max; + } + // 시간 복잡도: O(n) + // 공간 복잡도: O(n) +}; From 78b9cff42106f1308cf2b43cf6a886e14ac315c1 Mon Sep 17 00:00:00 2001 From: YeonguChoe Date: Wed, 25 Sep 2024 18:44:23 -0400 Subject: [PATCH 7/7] =?UTF-8?q?matrix[1..row][1..column]=200=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B0=94=EA=BE=B8=EB=8A=94=20=EB=B0=98=EB=B3=B5?= =?UTF-8?q?=EB=AC=B8=201=EA=B0=9C=EB=A1=9C=20=ED=95=A9=EC=B9=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- set-matrix-zeroes/yeonguchoe.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/set-matrix-zeroes/yeonguchoe.c b/set-matrix-zeroes/yeonguchoe.c index bea6246e8..7b9f3e45a 100644 --- a/set-matrix-zeroes/yeonguchoe.c +++ b/set-matrix-zeroes/yeonguchoe.c @@ -29,17 +29,9 @@ void setZeroes(int** matrix, int matrixSize, int* matrixColSize) { } } - for (int i = 1; i < column_size; i++) { - if (matrix[0][i] == 0) { - for (int j = 1; j < row_size; j++) { - matrix[j][i] = 0; - } - } - } - for (int i = 1; i < row_size; i++) { - if (matrix[i][0] == 0) { - for (int j = 1; j < column_size; j++) { + for (int j = 1; j < column_size; j++) { + if (matrix[0][j] == 0 || matrix[i][0] == 0) { matrix[i][j] = 0; } }