-
-
Notifications
You must be signed in to change notification settings - Fork 361
[sangbeenmoon] WEEK 07 Solutions #2805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,30 @@ def lengthOfLongestSubstring(self, s: str) -> int: | |
| answer = max(answer, i - start + 1) | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
| # ------ | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
|
|
||
| left = 0 | ||
| right = 0 | ||
|
|
||
| answer = 0 | ||
|
|
||
| dictionary = {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 적절한 네이밍 해주시면 좋을 거 같습니다 |
||
|
|
||
| for i,ch in enumerate(s): | ||
| right += 1 | ||
| if ch not in dictionary: | ||
| dictionary[ch] = i | ||
| else: | ||
| idx = dictionary[ch] | ||
| if left <= idx: | ||
| left = idx + 1 | ||
| dictionary[ch] = i | ||
|
|
||
| print(right, left) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그는 제거 부탁드립니다 |
||
| answer = max(answer, right - left) | ||
|
|
||
| return answer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석number-of-islands/sangbeenmoon.pyclass Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m,n = len(grid[0]), len(grid)
visited = [[False] * m for _ in range(n)]
dx = [0,0, -1, 1]
dy = [-1,1,0,0]
def dfs(xx:int, yy:int):
for d in range(4):
nx = xx + dx[d]
ny = yy + dy[d]
if 0 <= nx and nx < m and 0 <= ny and ny < n and grid[ny][nx] == "1":
if not visited[ny][nx]:
visited[ny][nx] = True
dfs(nx, ny)
answer = 0
for y in range(n):
for x in range(m):
if grid[y][x] == "1" and not visited[y][x]:
answer = answer + 1
dfs(x,y)
return answer
# ------
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
x_len , y_len = len(grid[0]), len(grid)
visited = [[False] * x_len for _ in range(y_len)]
dx = [0,0,-1,1]
dy = [-1,1,0,0]
def dfs(xx:int, yy:int):
for d in range(4):
nx = xx + dx[d]
ny = yy + dy[d]
if 0 <= nx and nx < x_len and 0 <= ny and ny < y_len and grid[ny][nx] == "1":
if not visited[ny][nx]:
visited[ny][nx] = True
dfs(nx,ny)
return
answer = 0
for xx in range(x_len):
for yy in range(y_len):
if grid[yy][xx] == "1" and not visited[yy][xx]:
answer += 1
visited[yy][xx] = True
dfs(xx,yy)
return answer
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(mn) |
피드백: 전체 격자를 순회하며 방문 여부를 추적하고 인접 노드를 재귀적으로 탐색한다.
개선 제안: 재귀 깊이가 큰 경우 스택 오버플로를 피하기 위해 BFS로 바꾸거나 수동 스택으로 구현 고려.
풀이 2: Solution.numIslands — Time: O(mn) / Space: O(mn)
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(mn) |
피드백: 2차원 방문 배열과 방향 벡터를 활용한 표준 DFS 구현이다.
개선 제안: 현 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,3 +27,53 @@ def dfs(xx:int, yy:int): | |
| answer = answer + 1 | ||
| dfs(x,y) | ||
| return answer | ||
|
|
||
| # ------ | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
|
|
||
| x_len , y_len = len(grid[0]), len(grid) | ||
|
|
||
| visited = [[False] * x_len for _ in range(y_len)] | ||
|
|
||
|
Comment on lines
+42
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별도의 visited 배열을 사용하지 않고, 방문한 grid의 값을 직접 변경하는 in-place 방식으로도 최적화가 가능해 보입니다! |
||
| dx = [0,0,-1,1] | ||
| dy = [-1,1,0,0] | ||
|
|
||
| def dfs(xx:int, yy:int): | ||
|
|
||
| for d in range(4): | ||
| nx = xx + dx[d] | ||
| ny = yy + dy[d] | ||
|
|
||
| if 0 <= nx and nx < x_len and 0 <= ny and ny < y_len and grid[ny][nx] == "1": | ||
| if not visited[ny][nx]: | ||
| visited[ny][nx] = True | ||
| dfs(nx,ny) | ||
|
|
||
| return | ||
|
|
||
| answer = 0 | ||
|
|
||
| for xx in range(x_len): | ||
| for yy in range(y_len): | ||
| if grid[yy][xx] == "1" and not visited[yy][xx]: | ||
| answer += 1 | ||
| visited[yy][xx] = True | ||
| dfs(xx,yy) | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석set-matrix-zeroes/sangbeenmoon.py# SC : O(m+n)
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m,n = len(matrix), len(matrix[0])
row_dict = {}
col_dict = {}
for r in range(m):
for c in range(n):
if matrix[r][c] == 0:
row_dict[r] = True
col_dict[c] = True
for r in range(m):
for c in range(n):
if r in row_dict or c in col_dict:
matrix[r][c] = 0
# ---------
# SC : O(1)
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
r_len , c_len = len(matrix), len(matrix[0])
set_zero_first_col = any(matrix[r][0] == 0 for r in range(r_len))
for r in range(r_len):
for c in range(1, c_len): # c는 1부터
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0
for r in range(1, r_len):
for c in range(1, c_len):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0
if matrix[0][0] == 0: # 0행 먼저
for c in range(c_len):
matrix[0][c] = 0
if set_zero_first_col: # 0열 나중
for r in range(r_len):
matrix[r][0] = 0
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(m + n) |
피드백: 초기 스캔에서 제로가 있는 행/열을 기록하고, 이후 한 번에 해당 행/열의 원소를 0으로 바꾼다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.setZeroes — Time: O(mn) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(1) |
피드백: 다음에 한 번의 스캔으로 원소를 0으로 바꾸기 위한 상위 상태를 유지한다.
개선 제안: 현 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,46 @@ def setZeroes(self, matrix: List[List[int]]) -> None: | |
| for c in range(n): | ||
| if r in row_dict or c in col_dict: | ||
| matrix[r][c] = 0 | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| # --------- | ||
|
|
||
| # SC : O(1) | ||
|
|
||
| class Solution: | ||
| def setZeroes(self, matrix: List[List[int]]) -> None: | ||
| """ | ||
| Do not return anything, modify matrix in-place instead. | ||
| """ | ||
| r_len , c_len = len(matrix), len(matrix[0]) | ||
|
|
||
| set_zero_first_col = any(matrix[r][0] == 0 for r in range(r_len)) | ||
|
|
||
| for r in range(r_len): | ||
| for c in range(1, c_len): # c는 1부터 | ||
| if matrix[r][c] == 0: | ||
| matrix[r][0] = 0 | ||
| matrix[0][c] = 0 | ||
|
|
||
|
Comment on lines
+42
to
+50
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. matrix[0][0]을 첫 번째 행의 마커처럼 활용하면 set_zero_first_row 같은 별도의 변수 없이도 처리할 수 있었군요! 🫢 |
||
| for r in range(1, r_len): | ||
| for c in range(1, c_len): | ||
| if matrix[r][0] == 0 or matrix[0][c] == 0: | ||
| matrix[r][c] = 0 | ||
|
|
||
| if matrix[0][0] == 0: # 0행 먼저 | ||
| for c in range(c_len): | ||
| matrix[0][c] = 0 | ||
|
|
||
|
|
||
| if set_zero_first_col: # 0열 나중 | ||
| for r in range(r_len): | ||
| matrix[r][0] = 0 | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석unique-paths/sangbeenmoon.py# dp[r][c] = dp[r-1][c] + dp[r][c-1]
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * n for _ in range(m)]
for r in range(m):
for c in range(n):
if r == 0 or c == 0:
continue
dp[r][c] = dp[r-1][c] + dp[r][c-1]
return dp[m-1][n-1]
# ------------
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * n for _ in range(m)]
for r in range(1,m):
for c in range(1,n):
dp[r][c] = dp[r-1][c] + dp[r][c-1]
return dp[m-1][n-1]
📊 시간/공간 복잡도 분석
풀이 1:
|
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(mn) |
피드백: 초기화된 1의 행/열에서 시작해 중복 없는 경로 수를 누적한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.uniquePaths — Time: O(mn) / Space: O(mn)
| 복잡도 | |
|---|---|
| Time | O(mn) |
| Space | O(mn) |
피드백: 직관적으로 풀이가 명확하며 시간/공간 복잡도도 일반적인 해법과 같다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,17 @@ def uniquePaths(self, m: int, n: int) -> int: | |
| dp[r][c] = dp[r-1][c] + dp[r][c-1] | ||
|
|
||
| return dp[m-1][n-1] | ||
|
|
||
|
|
||
|
|
||
| # ------------ | ||
|
|
||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| dp = [[1] * n for _ in range(m)] | ||
|
|
||
| for r in range(1,m): | ||
| for c in range(1,n): | ||
| dp[r][c] = dp[r-1][c] + dp[r][c-1] | ||
|
Comment on lines
+21
to
+25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 2차원 DP를 사용하고 있는데, 1차원 DP로 공간 복잡도를 최적화해서 풀어보셔도 좋을 것 같습니다! |
||
|
|
||
| return dp[m-1][n-1] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
longest-substring-without-repeating-characters/sangbeenmoon.py
📊 시간/공간 복잡도 분석
풀이 1:
Solution.lengthOfLongestSubstring— Time: O(n) / Space: O(min(n, σ))피드백: 각 문자 마지막 인덱스를 기록하고 윈도우 시작을 중복 위치 바로 다음으로 옮겨 중복 없이 구간을 확장한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.lengthOfLongestSubstring— Time: O(n) / Space: O(min(n, σ))피드백: 딕셔너리에 최근 위치를 저장하고 중복 시 왼쪽 포인터를 갱신한다. 다만 불필요한 출력(print)이 있어 성능/출력 측면에서 제거 권장.
개선 제안: 출력문 제거 후 최적화 가능.