-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from YeonguChoe/main
[영우] Week7 Solutions
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
longest-substring-without-repeating-characters/yeonguchoe.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<char> 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) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class Solution { | ||
public: | ||
int numIslands(vector<vector<char>>& grid) { | ||
int count = 0; | ||
|
||
int row_size = grid.size(); | ||
int column_size = grid[0].size(); | ||
|
||
queue<pair<int, int>> 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<vector<char>>& grid, | ||
queue<pair<int, int>>& q, int& row_size, int& column_size) { | ||
pair<int, int> center_location{x, y}; | ||
q.push(center_location); | ||
|
||
grid[x][y] = '0'; | ||
|
||
while (!q.empty()) { | ||
pair<int, int> 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) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}; |