Skip to content

Commit d94393f

Browse files
authored
Merge pull request #486 from obzva/main
[Flynn] Week7
2 parents 6dd8755 + 2f11ec0 commit d94393f

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* ํ’€์ด
3+
* - ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด `s`๋ฅผ ํ•œ ๋ฒˆ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค
4+
* - lookup์ด๋ผ๋Š” ํ•ด์‹œ๋งต ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜„์žฌ ์กฐํšŒํ•˜๊ณ  ์žˆ๋Š”
5+
* substring์— ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค
6+
*
7+
* Big O
8+
* - N: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด `s`์˜ ๊ธธ์ด
9+
*
10+
* - Time complexity: O(N)
11+
* - Space complexity: O(N)
12+
*/
13+
14+
class Solution {
15+
public:
16+
int lengthOfLongestSubstring(string s) {
17+
if (s.size() == 0) return 0;
18+
19+
unordered_map<char, int> lookup;
20+
lookup.insert({s[0], 0});
21+
22+
int res = 1;
23+
24+
int start = 0;
25+
int end = 1;
26+
27+
while (end < s.size()) {
28+
if (lookup.find(s[end]) != lookup.end()
29+
&& lookup[s[end]] >= start) {
30+
start = lookup[s[end]] + 1;
31+
}
32+
33+
lookup[s[end]] = end;
34+
35+
res = max(res, end - start + 1);
36+
37+
++end;
38+
}
39+
40+
return res;
41+
}
42+
};

โ€Žnumber-of-islands/flynn.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* ํ’€์ด
3+
* - bfs๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค
4+
*
5+
* Big O
6+
* - M: grid์˜ ํ–‰์˜ ์ˆ˜
7+
* - N: grid์˜ ์—ด์˜ ์ˆ˜
8+
*
9+
* - Time complexity: O(MN)
10+
* - ๊ฐ ์ขŒํ‘œ๋Š” ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ์กฐํšŒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค
11+
* - Space complexity: O(MN)
12+
* - ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•ด visit ๋ฐฐ์—ด์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค
13+
* - queue์— ์Œ“์ด๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ MN๊ฐœ๊นŒ์ง€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
14+
*/
15+
16+
class Solution {
17+
public:
18+
int numIslands(vector<vector<char>>& grid) {
19+
int m = grid.size();
20+
int n = grid[0].size();
21+
22+
vector<vector<bool>> visit;
23+
for (int r = 0; r < m; ++r) {
24+
vector<bool> row;
25+
for (int c = 0; c < n; ++c) {
26+
row.push_back(false);
27+
}
28+
visit.push_back(row);
29+
}
30+
31+
pair<int, int> dirs[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
32+
33+
int res = 0;
34+
queue<pair<int, int>> q;
35+
for (int r = 0; r < m; ++r) {
36+
for (int c = 0; c < n; ++c) {
37+
if (visit[r][c] == false && grid[r][c] == '1') {
38+
++res;
39+
q.push({r, c});
40+
while (!q.empty()) {
41+
auto p = q.front();
42+
q.pop();
43+
for (auto dir : dirs) {
44+
pair<int, int> next = {p.first + dir.first, p.second + dir.second};
45+
if (0 <= next.first && next.first < m && 0 <= next.second && next.second < n) {
46+
if (visit[next.first][next.second] == false && grid[next.first][next.second] == '1') {
47+
q.push(next);
48+
visit[next.first][next.second] = true;
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
return res;
58+
}
59+
};

โ€Žreverse-linked-list/flynn.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* ํ’€์ด
3+
* - ์ƒ๋žต, ์ฃผ์„ ์ฐธ๊ณ 
4+
*
5+
* Big O
6+
* - N: ์ฃผ์–ด์ง„ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ `head`์˜ ๊ธธ์ด
7+
*
8+
* - Time complexity: O(N)
9+
* - Space complexity: O(1)
10+
*/
11+
12+
/**
13+
* Definition for singly-linked list.
14+
* struct ListNode {
15+
* int val;
16+
* ListNode *next;
17+
* ListNode() : val(0), next(nullptr) {}
18+
* ListNode(int x) : val(x), next(nullptr) {}
19+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
ListNode* reverseList(ListNode* head) {
25+
if (head == nullptr) return head;
26+
// example: a - b - c - d
27+
28+
ListNode* root = new ListNode();
29+
root->next = head;
30+
// root - a - b - c - d
31+
32+
ListNode* p = head;
33+
ListNode* q = p->next;
34+
// root - a - b - c - d
35+
// (p) (q)
36+
37+
while (q != nullptr) {
38+
p->next = q->next;
39+
// root - a - c - d
40+
// b /
41+
q->next = root->next;
42+
// root - a - c - d
43+
// b /
44+
root->next = q;
45+
// root - b - a - c - d
46+
// (q) (p)
47+
q = p->next;
48+
// root - b - a - c - d
49+
// (p) (q)
50+
}
51+
52+
return root->next;
53+
}
54+
};

โ€Žset-matrix-zeroes/flynn.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* ํ’€์ด
3+
* - matrix ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•˜๋‹ค๊ฐ€ ๊ฐ’์ด 0์ธ ์ขŒํ‘œ๋ฅผ ์ฐพ์œผ๋ฉด
4+
* ๊ทธ ์ขŒํ‘œ์˜ ์ฒซ๋ฒˆ์งธ ์—ด, ์ฒซ๋ฒˆ์งธ ํ–‰ ์ขŒํ‘œ์— ํ‘œ์‹œ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค
5+
* if matrix[r][c] == 0
6+
* matrix[r][0] = 0
7+
* matrix[0][c] = 0
8+
* - ๋งŒ์•ฝ ํ•ด๋‹น ์ขŒํ‘œ๊ฐ€ ์ฒซ๋ฒˆ์งธ ํ–‰์ด๊ฑฐ๋‚˜ ์ฒซ๋ฒˆ์งธ ์—ด์ด๋ผ๋ฉด ๋”ฐ๋กœ ๊ธฐ๋กํ•ด๋‘ก๋‹ˆ๋‹ค
9+
* - ์ฒซ๋ฒˆ์งธ ์™„์ „ํƒ์ƒ‰์„ ๋งˆ์นœ ํ›„์—” matrix์˜ ์ฒซ๋ฒˆ์งธ ํ–‰, ์—ด์„ ๋ณด๊ณ  ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค
10+
*
11+
* Big O
12+
* - M: matrix์˜ ํ–‰ ๊ฐœ์ˆ˜
13+
* - N: matrix์˜ ์—ด ๊ฐœ์ˆ˜
14+
*
15+
* - Time complexity: O(MN)
16+
* - Space complexity: O(1)
17+
*
18+
*/
19+
20+
class Solution {
21+
public:
22+
void setZeroes(vector<vector<int>>& matrix) {
23+
int m = matrix.size();
24+
int n = matrix[0].size();
25+
26+
bool first_row = false;
27+
bool first_col = false;
28+
29+
for (int r = 0; r < m; ++r) {
30+
for (int c = 0; c < n; ++c) {
31+
if (!matrix[r][c]) {
32+
if (!r) first_row = true;
33+
if (!c) first_col = true;
34+
matrix[r][0] = 0;
35+
matrix[0][c] = 0;
36+
}
37+
}
38+
}
39+
40+
for (int r = 1; r < m; ++r) {
41+
if (!matrix[r][0]) {
42+
for (int c = 1; c < n; ++c) matrix[r][c] = 0;
43+
}
44+
}
45+
46+
for (int c = 1; c < n; ++c) {
47+
if (!matrix[0][c]) {
48+
for (int r = 1; r < m; ++r) matrix[r][c] = 0;
49+
}
50+
}
51+
52+
if (first_row) {
53+
for (int c = 0; c < n; ++c) matrix[0][c] = 0;
54+
}
55+
56+
if (first_col) {
57+
for (int r = 0; r < m; ++r) matrix[r][0] = 0;
58+
}
59+
}
60+
};

โ€Žunique-paths/flynn.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* ํ’€์ด
3+
* - ์กฐํ•ฉ ๊ณต์‹์„ ์‚ฌ์šฉํ•˜๋ฉด overflow ๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
4+
* - ๋ชจ๋“  ์ขŒํ‘œ์— ๋Œ€ํ•ด uniquePaths๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค
5+
* - ํŠน์ • ์ขŒํ‘œ์˜ uniquePaths๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ํ–‰๋งŒ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธธ์ด m์˜ ๋ฐฐ์—ด ๋‘ ๊ฐœ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค
6+
*
7+
* Big O
8+
* - Time complexity: O(MN)
9+
* - Space compexity: O(N)
10+
*/
11+
12+
class Solution {
13+
public:
14+
int uniquePaths(int m, int n) {
15+
vector<int> row1;
16+
vector<int> row2;
17+
18+
for (int i = 0; i < n; ++i) row1.push_back(1);
19+
row2.push_back(1);
20+
for (int i = 1; i < n; ++i) row2.push_back(0);
21+
22+
for (int j = 1; j < m; ++j) {
23+
for (int i = 1; i < n; ++i) row2[i] = row1[i] + row2[i - 1];
24+
swap(row1, row2);
25+
row2[0] = 1;
26+
for (int i = 1; i < n; ++i) row2[i] = 0;
27+
}
28+
29+
return row1[n - 1];
30+
}
31+
};

0 commit comments

Comments
ย (0)