-
-
Notifications
You must be signed in to change notification settings - Fork 245
[영우] Week7 Solutions #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[영우] Week7 Solutions #497
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2721b1
adds week7
YeonguChoe c901528
week7
YeonguChoe b664c6c
week 7
YeonguChoe 2ef7791
week7
YeonguChoe 9869f6b
Create yeonguchoe.cpp
YeonguChoe fe8c66a
adds week 7
ac96dc7
Merge branch 'sub-branch'
78b9cff
matrix[1..row][1..column] 0으로 바꾸는 반복문 1개로 합침.
YeonguChoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
longest-substring-without-repeating-characters/yeonguchoe.cpp
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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) |
This file contains hidden or 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) | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.