Skip to content

Commit

Permalink
Merge pull request #486 from obzva/main
Browse files Browse the repository at this point in the history
[Flynn] Week7
  • Loading branch information
obzva authored Sep 26, 2024
2 parents 6dd8755 + 2f11ec0 commit d94393f
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
42 changes: 42 additions & 0 deletions longest-substring-without-repeating-characters/flynn.cpp
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;
}
};
59 changes: 59 additions & 0 deletions number-of-islands/flynn.cpp
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;
}
};
54 changes: 54 additions & 0 deletions reverse-linked-list/flynn.cpp
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;
}
};
60 changes: 60 additions & 0 deletions set-matrix-zeroes/flynn.cpp
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;
}
}
};
31 changes: 31 additions & 0 deletions unique-paths/flynn.cpp
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];
}
};

0 comments on commit d94393f

Please sign in to comment.