Skip to content

Commit

Permalink
Merge pull request #937 from Chaedie/main
Browse files Browse the repository at this point in the history
[Chaedie] Week 7
  • Loading branch information
TonyKim9401 authored Jan 25, 2025
2 parents eb43e21 + b0c8ddf commit 3acc058
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions longest-substring-without-repeating-characters/Chaedie.py
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
31 changes: 31 additions & 0 deletions number-of-islands/Chaedie.py
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
21 changes: 21 additions & 0 deletions reverse-linked-list/Chaedie.py
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
31 changes: 31 additions & 0 deletions set-matrix-zeroes/Chaedie.py
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
33 changes: 33 additions & 0 deletions unique-paths/Chaedie.py
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]

0 comments on commit 3acc058

Please sign in to comment.