-
Notifications
You must be signed in to change notification settings - Fork 126
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 #486 from obzva/main
[Flynn] Week7
- Loading branch information
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
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,42 @@ | ||
/** | ||
* ํ์ด | ||
* - ์ฃผ์ด์ง ๋ฌธ์์ด `s`๋ฅผ ํ ๋ฒ ์กฐํํฉ๋๋ค | ||
* - lookup์ด๋ผ๋ ํด์๋งต ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ํ์ฌ ์กฐํํ๊ณ ์๋ | ||
* substring์ ๋ฐ๋ณต๋๋ ๋ฌธ์๊ฐ ์๋์ง ๊ฒ์ฌํฉ๋๋ค | ||
* | ||
* Big O | ||
* - N: ์ฃผ์ด์ง ๋ฌธ์์ด `s`์ ๊ธธ์ด | ||
* | ||
* - Time complexity: O(N) | ||
* - Space complexity: O(N) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int lengthOfLongestSubstring(string s) { | ||
if (s.size() == 0) return 0; | ||
|
||
unordered_map<char, int> lookup; | ||
lookup.insert({s[0], 0}); | ||
|
||
int res = 1; | ||
|
||
int start = 0; | ||
int end = 1; | ||
|
||
while (end < s.size()) { | ||
if (lookup.find(s[end]) != lookup.end() | ||
&& lookup[s[end]] >= start) { | ||
start = lookup[s[end]] + 1; | ||
} | ||
|
||
lookup[s[end]] = end; | ||
|
||
res = max(res, end - start + 1); | ||
|
||
++end; | ||
} | ||
|
||
return res; | ||
} | ||
}; |
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,59 @@ | ||
/** | ||
* ํ์ด | ||
* - bfs๋ฅผ ํ์ฉํฉ๋๋ค | ||
* | ||
* Big O | ||
* - M: grid์ ํ์ ์ | ||
* - N: grid์ ์ด์ ์ | ||
* | ||
* - Time complexity: O(MN) | ||
* - ๊ฐ ์ขํ๋ ์ต๋ ํ ๋ฒ์ฉ๋ง ์กฐํํ๊ฒ ๋ฉ๋๋ค | ||
* - Space complexity: O(MN) | ||
* - ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ๊ธฐ๋กํ๊ธฐ ์ํด visit ๋ฐฐ์ด์ด ์ฌ์ฉ๋ฉ๋๋ค | ||
* - queue์ ์์ด๋ ์์์ ๊ฐ์๋ ์ต๋ MN๊ฐ๊น์ง ์ฆ๊ฐํ ์ ์์ต๋๋ค | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int numIslands(vector<vector<char>>& grid) { | ||
int m = grid.size(); | ||
int n = grid[0].size(); | ||
|
||
vector<vector<bool>> visit; | ||
for (int r = 0; r < m; ++r) { | ||
vector<bool> row; | ||
for (int c = 0; c < n; ++c) { | ||
row.push_back(false); | ||
} | ||
visit.push_back(row); | ||
} | ||
|
||
pair<int, int> dirs[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; | ||
|
||
int res = 0; | ||
queue<pair<int, int>> q; | ||
for (int r = 0; r < m; ++r) { | ||
for (int c = 0; c < n; ++c) { | ||
if (visit[r][c] == false && grid[r][c] == '1') { | ||
++res; | ||
q.push({r, c}); | ||
while (!q.empty()) { | ||
auto p = q.front(); | ||
q.pop(); | ||
for (auto dir : dirs) { | ||
pair<int, int> next = {p.first + dir.first, p.second + dir.second}; | ||
if (0 <= next.first && next.first < m && 0 <= next.second && next.second < n) { | ||
if (visit[next.first][next.second] == false && grid[next.first][next.second] == '1') { | ||
q.push(next); | ||
visit[next.first][next.second] = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
}; |
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,54 @@ | ||
/** | ||
* ํ์ด | ||
* - ์๋ต, ์ฃผ์ ์ฐธ๊ณ | ||
* | ||
* Big O | ||
* - N: ์ฃผ์ด์ง ๋งํฌ๋ ๋ฆฌ์คํธ `head`์ ๊ธธ์ด | ||
* | ||
* - Time complexity: O(N) | ||
* - Space complexity: O(1) | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* reverseList(ListNode* head) { | ||
if (head == nullptr) return head; | ||
// example: a - b - c - d | ||
|
||
ListNode* root = new ListNode(); | ||
root->next = head; | ||
// root - a - b - c - d | ||
|
||
ListNode* p = head; | ||
ListNode* q = p->next; | ||
// root - a - b - c - d | ||
// (p) (q) | ||
|
||
while (q != nullptr) { | ||
p->next = q->next; | ||
// root - a - c - d | ||
// b / | ||
q->next = root->next; | ||
// root - a - c - d | ||
// b / | ||
root->next = q; | ||
// root - b - a - c - d | ||
// (q) (p) | ||
q = p->next; | ||
// root - b - a - c - d | ||
// (p) (q) | ||
} | ||
|
||
return root->next; | ||
} | ||
}; |
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,60 @@ | ||
/** | ||
* ํ์ด | ||
* - matrix ์ ์ฒด๋ฅผ ํ์ํ๋ค๊ฐ ๊ฐ์ด 0์ธ ์ขํ๋ฅผ ์ฐพ์ผ๋ฉด | ||
* ๊ทธ ์ขํ์ ์ฒซ๋ฒ์งธ ์ด, ์ฒซ๋ฒ์งธ ํ ์ขํ์ ํ์๋ฅผ ํฉ๋๋ค | ||
* if matrix[r][c] == 0 | ||
* matrix[r][0] = 0 | ||
* matrix[0][c] = 0 | ||
* - ๋ง์ฝ ํด๋น ์ขํ๊ฐ ์ฒซ๋ฒ์งธ ํ์ด๊ฑฐ๋ ์ฒซ๋ฒ์งธ ์ด์ด๋ผ๋ฉด ๋ฐ๋ก ๊ธฐ๋กํด๋ก๋๋ค | ||
* - ์ฒซ๋ฒ์งธ ์์ ํ์์ ๋ง์น ํ์ matrix์ ์ฒซ๋ฒ์งธ ํ, ์ด์ ๋ณด๊ณ ๋ฌธ์ ์์ ์๊ตฌํ๋ ๋ฐ๋ฅผ ์ํํฉ๋๋ค | ||
* | ||
* Big O | ||
* - M: matrix์ ํ ๊ฐ์ | ||
* - N: matrix์ ์ด ๊ฐ์ | ||
* | ||
* - Time complexity: O(MN) | ||
* - Space complexity: O(1) | ||
* | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
int m = matrix.size(); | ||
int n = matrix[0].size(); | ||
|
||
bool first_row = false; | ||
bool first_col = false; | ||
|
||
for (int r = 0; r < m; ++r) { | ||
for (int c = 0; c < n; ++c) { | ||
if (!matrix[r][c]) { | ||
if (!r) first_row = true; | ||
if (!c) first_col = true; | ||
matrix[r][0] = 0; | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
for (int r = 1; r < m; ++r) { | ||
if (!matrix[r][0]) { | ||
for (int c = 1; c < n; ++c) matrix[r][c] = 0; | ||
} | ||
} | ||
|
||
for (int c = 1; c < n; ++c) { | ||
if (!matrix[0][c]) { | ||
for (int r = 1; r < m; ++r) matrix[r][c] = 0; | ||
} | ||
} | ||
|
||
if (first_row) { | ||
for (int c = 0; c < n; ++c) matrix[0][c] = 0; | ||
} | ||
|
||
if (first_col) { | ||
for (int r = 0; r < m; ++r) matrix[r][0] = 0; | ||
} | ||
} | ||
}; |
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,31 @@ | ||
/** | ||
* ํ์ด | ||
* - ์กฐํฉ ๊ณต์์ ์ฌ์ฉํ๋ฉด overflow ๋ฐ ์๊ฐ์ด๊ณผ๋ฅผ ์ผ์ผํฌ ์ ์์ต๋๋ค | ||
* - ๋ชจ๋ ์ขํ์ ๋ํด uniquePaths๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค | ||
* - ํน์ ์ขํ์ uniquePaths๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์๋ ๋ ํ๋ง ํ์ํ๊ธฐ ๋๋ฌธ์ ๊ธธ์ด m์ ๋ฐฐ์ด ๋ ๊ฐ๋ฅผ ์ด์ฉํฉ๋๋ค | ||
* | ||
* Big O | ||
* - Time complexity: O(MN) | ||
* - Space compexity: O(N) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int uniquePaths(int m, int n) { | ||
vector<int> row1; | ||
vector<int> row2; | ||
|
||
for (int i = 0; i < n; ++i) row1.push_back(1); | ||
row2.push_back(1); | ||
for (int i = 1; i < n; ++i) row2.push_back(0); | ||
|
||
for (int j = 1; j < m; ++j) { | ||
for (int i = 1; i < n; ++i) row2[i] = row1[i] + row2[i - 1]; | ||
swap(row1, row2); | ||
row2[0] = 1; | ||
for (int i = 1; i < n; ++i) row2[i] = 0; | ||
} | ||
|
||
return row1[n - 1]; | ||
} | ||
}; |