-
-
Notifications
You must be signed in to change notification settings - Fork 202
[Lyla] Week 07 #926
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
[Lyla] Week 07 #926
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
''' | ||
시간복잡도: O(n) | ||
- right와 left 포인터가 각각 최대 n번 움직임 | ||
|
||
공간복잡도: O(n) | ||
- hars 집합이 최대 n개의 고유 문자를 저장할 수 있 | ||
''' | ||
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
length = 0 | ||
left = 0 | ||
chars = set() | ||
|
||
for right in range(len(s)): | ||
if s[right] in chars: | ||
while s[right] in chars: | ||
chars.remove(s[left]) | ||
left += 1 | ||
chars.add(s[right]) | ||
length = max(length, right + 1 - left) | ||
|
||
return length |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
''' | ||
시간 복잡도: O(m * n) | ||
- 각 셀을 한 번씩 방문하며, DFS를 통해 연결된 모든 셀을 확인하므로 grid의 모든 셀에 대해 O(1) 작업이 수행됩니다. | ||
|
||
공간 복잡도: O(m * n) | ||
- 최악의 경우, DFS의 호출 스택이 격자의 모든 셀에 대해 쌓일 수 있습니다. 이 경우 스택에 저장되는 셀의 개수는 최대 m * n 입니다. | ||
''' | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def numIslands(self, grid: List[List[str]]) -> int: | ||
count = 0 | ||
m, n = len(grid), len(grid[0]) | ||
|
||
def exploreIsland(row: int, col: int): | ||
withinBounds = (0 <= row < m) and (0 <= col < n) | ||
if not withinBounds or grid[row][col] != "1": | ||
return | ||
|
||
grid[row][col] = "#" | ||
|
||
exploreIsland(row + 1, col) | ||
exploreIsland(row - 1, col) | ||
exploreIsland(row, col + 1) | ||
exploreIsland(row, col - 1) | ||
|
||
# Iterate through all cells in the grid | ||
for i in range(m): | ||
for j in range(n): | ||
# if a cell is "1", increment count and mark the whole island as "#" | ||
if grid[i][j] == "1": | ||
count += 1 | ||
exploreIsland(i, j) | ||
|
||
return count |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
''' | ||
시간 복잡도: O(n) | ||
- 리스트의 모든 노드를 한 번씩 방문하므로 시간 복잡도는 O(n)입니다. | ||
|
||
공간 복잡도: O(n) | ||
- 재귀 호출을 사용하므로 호출 스택에 최대 n번의 함수 호출이 쌓이므로 공간 복잡도는 O(n)입니다. | ||
''' | ||
from typing import Optional | ||
|
||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution: | ||
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if not head or not head.next: | ||
return head | ||
|
||
new_head = self.reverseList(head.next) # find the last node | ||
head.next.next = head # reverse | ||
head.next = None # remove cycle | ||
|
||
return new_head |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
''' | ||
시간복잡도: O(m * n) | ||
공간복잡도: O(1) | ||
''' | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def setZeroes(self, matrix: List[List[int]]) -> None: | ||
m, n = len(matrix), len(matrix[0]) | ||
first_row_zero = False # Flag to check if the first row needs to be zeroed | ||
first_col_zero = False # Flag to check if the first column needs to be zeroed | ||
|
||
# Check if the first row has any zeros | ||
for j in range(n): | ||
if matrix[0][j] == 0: | ||
first_row_zero = True | ||
break | ||
|
||
# Check if the first column has any zeros | ||
for i in range(m): | ||
if matrix[i][0] == 0: | ||
first_col_zero = True | ||
break | ||
|
||
# Use the first row and column to mark rows and columns that need to be zeroed | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
if matrix[i][j] == 0: | ||
matrix[i][0] = 0 | ||
matrix[0][j] = 0 | ||
|
||
# Zero out cells based on markers in the first row and column | ||
for i in range(1, m): | ||
for j in range(1, n): | ||
if matrix[i][0] == 0 or matrix[0][j] == 0: | ||
matrix[i][j] = 0 | ||
|
||
# Zero out the first row if needed | ||
if first_row_zero: | ||
for j in range(n): | ||
matrix[0][j] = 0 | ||
|
||
# Zero out the first column if needed | ||
if first_col_zero: | ||
for i in range(m): | ||
matrix[i][0] = 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
''' | ||
시간 복잡도: O(m * n) | ||
- 동적 프로그래밍 테이블(dp)을 사용하여 각 셀에서의 경로 수를 한 번씩 계산하므로 시간 복잡도는 격자의 모든 셀에 대해 O(m * n)입니다. | ||
|
||
공간 복잡도: O(m * n) | ||
- dp 테이블을 사용하여 모든 셀에 대한 경로 수를 저장하므로 공간 복잡도는 O(m * n)입니다. | ||
Comment on lines
+5
to
+6
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. O(m)으로 최적화할 수 있을까요? 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차원 배열을 유지할 필요 없이, 한 행만 유지하면서 정보를 갱신하면 되겠네요! 피드백 감사합니다 :) class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [1] * n
for row in range(1, m):
for col in range(1, n):
dp[col] += dp[col - 1]
return dp[-1] 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. 감사합니다~! |
||
''' | ||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
# save number of unique paths to each cell | ||
dp = [[1] * n for _ in range(m)] | ||
|
||
for row in range(1, m): | ||
for col in range(1, n): | ||
dp[row][col] = dp[row][col - 1] + dp[row - 1][col] | ||
|
||
return dp[m - 1][n - 1] |
Uh oh!
There was an error while loading. Please reload this page.
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.
while문으로 중복되는 문자를 찾을 때까지 한 문자 한 문자 지우는 로직을 좀 더 단순하게 바꿀 수 있을 것 같아요
저라면 set으로 chars를 관리하는 것 대신에 dictionary로 관리할 것 같습니다
이러면 연산량이 좀 줄어들어요