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) +}; 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) +}; 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) diff --git a/set-matrix-zeroes/yeonguchoe.c b/set-matrix-zeroes/yeonguchoe.c new file mode 100644 index 000000000..7b9f3e45a --- /dev/null +++ b/set-matrix-zeroes/yeonguchoe.c @@ -0,0 +1,53 @@ +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 < row_size; i++) { + for (int j = 1; j < column_size; j++) { + if (matrix[0][j] == 0 || matrix[i][0] == 0) { + 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) 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) +};