-
Notifications
You must be signed in to change notification settings - Fork 125
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 #937 from Chaedie/main
[Chaedie] Week 7
- Loading branch information
Showing
5 changed files
with
138 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,22 @@ | ||
""" | ||
Solution: | ||
1) for iteration ์ ๋๋ ๋์ hash set ์ ์ด์ฉํด ์ฒ์ ๋ฐ๊ฒฌํ ์์๋ค์ window set์ ๋ฃ๋๋ค. | ||
2) ์ค๋ณต๋๋ ์์๋ฅผ ๋ฐ๊ฒฌํ ๊ฒฝ์ฐ ํด๋น ์์์ ์ค๋ณต์ด ์ฌ๋ผ์ง๋๊น์ง left side ์ ์์๋ค์ ํ๋์ฉ ์ ๊ฑฐํ๋ค. | ||
Time: O(n^2) = O(n) (for iteration) * O(n) ์ต์ ์ ๊ฒฝ์ฐ n๋งํผ์ ์ค๋ณต์ ๊ฑฐ | ||
Space: O(n) (๋ชจ๋ ์์๊ฐ set์ ๋ค์ด๊ฐ ๊ฒฝ์ฐ) | ||
""" | ||
|
||
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
window = set() | ||
max_len = 0 | ||
l = 0 | ||
for r in range(len(s)): | ||
while s[r] in window: | ||
window.remove(s[l]) | ||
l += 1 | ||
window.add(s[r]) | ||
max_len = max(max_len, len(window)) | ||
return max_len |
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 @@ | ||
""" | ||
Solution: | ||
1) for ๋ฌธ์ ๋๋ฉด์ ์ฌ(1)์ธ ๊ฒฝ์ฐ result ์ 1์ ๋ํ๊ณ dfs๋ฅผ ๋๋ฆฐ๋ค. | ||
2) dfs ๋ฅผ ํตํด ์ฌ ์ ์ฒด๋ฅผ ์ํํ๋ฉฐ 0์ผ๋ก ๋ง๋ค์ด์ค๋ค. | ||
3) ์ฌ์ ๊ฐฏ์ result ๋ฅผ return ํ๋ค. | ||
์ก์ง์ ๊ฐฏ์ n | ||
Time: O(n) nํ์ dfs ํจ์ ์คํ ๋ ์ ์์ | ||
Space: O(n) n์ ํธ์ถ์คํ์ด ์ฌ์ฉ ๋ ์ ์์ | ||
""" | ||
|
||
|
||
class Solution: | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
ROWS, COLS = len(grid), len(grid[0]) | ||
result = 0 | ||
|
||
def dfs(i, j): | ||
if i < 0 or i >= ROWS or j < 0 or j >= COLS or grid[i][j] == "0": | ||
return | ||
grid[i][j] = "0" | ||
for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]: | ||
dfs(i + dx, j + dy) | ||
|
||
for i in range(ROWS): | ||
for j in range(COLS): | ||
if grid[i][j] == "1": | ||
dfs(i, j) | ||
result += 1 | ||
|
||
return result |
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,21 @@ | ||
""" | ||
Solution: | ||
1) next node๋ฅผ ์ ์ฅํฉ๋๋ค. | ||
2) cur node ๋ฅผ prev node ๋ก ์ฐ๊ฒฐ์ํต๋๋ค. | ||
3) cur node ๊ฐ prev node ๊ฐ ๋ฉ๋๋ค. | ||
4) cur node ๋ next node ๊ฐ ๋ฉ๋๋ค. | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
prev = None | ||
cur = head | ||
while cur: | ||
next = cur.next | ||
cur.next = prev | ||
prev = cur | ||
cur = next | ||
return prev |
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 @@ | ||
""" | ||
Solution: | ||
1) matrix๋ฅผ ์ํํ๋ฉด์ 0์ ์ฐพ๋๋ค. | ||
2) 0์ผ ๊ฒฝ์ฐ rows, cols set์ ๊ฐ ์ธ๋ฑ์ค๋ฅผ ๋ฃ๋๋ค. | ||
3) rows, cols ๋ฅผ ์ํํ๋ฉด์ ํด๋นํ๋ row, col์ 0์ผ๋ก ๋ง๋ค์ด์ค๋ค. | ||
Time: O(nm) = O(nm) (์ํ) + ์ต๋ O(nm) + ์ต๋ O(nm) | ||
Space: O(n + m) | ||
""" | ||
|
||
|
||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
""" | ||
Do not return anything, modify matrix in-place instead. | ||
""" | ||
ROWS, COLS = len(matrix), len(matrix[0]) | ||
rows = set() | ||
cols = set() | ||
for i in range(ROWS): | ||
for j in range(COLS): | ||
if matrix[i][j] == 0: | ||
rows.add(i) | ||
cols.add(j) | ||
|
||
for i in rows: | ||
for j in range(COLS): | ||
matrix[i][j] = 0 | ||
|
||
for j in cols: | ||
for i in range(ROWS): | ||
matrix[i][j] = 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,33 @@ | ||
""" | ||
1 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 | ||
0 0 0 0 0 0 0 | ||
Solution: | ||
1) grid์์ 0,0์ ์ ์ธํ๊ณค ๋๋ฌํ ์ ์๋ ๋ฐฉ๋ฒ์ด left way + upper way ๊ฐฏ์๋ฐ์ ์๋ค๊ณ ์๊ฐํ๋ค. | ||
2) ๋ฐ๋ผ์ grid๋ฅผ ์ํํ๋ฉฐ grid index์ ๊ฒฝ๊ณ๋ฅผ ๋์ด๊ฐ์ง ์์ ๊ฒฝ์ฐ left way + upper way ๊ฐฏ์๋ฅผ ๋ํด์ฃผ์๋ค. | ||
3) ๋ง์ง๋ง grid์ ์ฐํ๋จ์ ๊ฐ์ return ํด์ฃผ์์ต๋๋ค. | ||
Time: O(mn) (์์์ ๊ฐฏ์) | ||
Space: O(mn) (์์์ ๊ฐฏ์) | ||
""" | ||
|
||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
dp = [] | ||
for i in range(m): | ||
dp.append([0] * n) | ||
print(dp) | ||
|
||
for i in range(m): | ||
for j in range(n): | ||
if i == 0 and j == 0: | ||
dp[i][j] = 1 | ||
continue | ||
|
||
up_value = 0 if i - 1 < 0 or i - 1 >= m else dp[i - 1][j] | ||
left_value = 0 if j - 1 < 0 or j - 1 >= n else dp[i][j - 1] | ||
|
||
dp[i][j] = up_value + left_value | ||
|
||
return dp[m - 1][n - 1] |