Skip to content
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

[yeoju] Week 7 #938

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions longest-substring-without-repeating-characters/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TC:O(n^2) SC:O(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 @aa601 님,
SC 를 O(1)로 적어주셨는데, 저의 경우 최악의 케이스에서 n개가 set()에 들어갈 경우 O(n)이 되는게 아닌가 생각했었는데 혹시 O(1)로 생각하신 이유가 궁금합니다~!

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
max_size = 0
for start in range(len(s)):
saw = set()
for end in range(start, len(s)):
if s[end] in saw:
break
else:
saw.add(s[end])
max_size = max(max_size, end - start + 1) # 부분 문자열의 길이와 현재까지의 최대길이값 비교
return max_size
20 changes: 20 additions & 0 deletions number-of-islands/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TC:O(n * m), SC:O(n * m)
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
cnt = 0
row = len(grid)
col = len(grid[0])
def dfs(r: int, c: int) :
grid[r][c] = "0"
for y, x in [(r, c + 1), (r + 1, c), (r - 1, c), (r, c - 1)]: # 현재 r,c 좌표에 대해 상하좌우 탐색
if 0 <= y < row and 0 <= x < col:
if grid[y][x] == "1":
dfs(y, x)
return

for r in range(row):
for c in range(col):
if grid[r][c] == "1": #땅 발견 시 cnt 증가하고, 발견된 땅과 연결된 땅들을 제거
cnt += 1
dfs(r, c)
return cnt
11 changes: 11 additions & 0 deletions reverse-linked-list/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TC:O(n), SC:O(1)
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
prv = None
while cur != None:
tmp = cur.next # 다음 노드의 주소를 tmp에 저장
cur.next = prv # 현재 노드가 가리키는 주소를 prv로 설정
prv = cur # prv를 현재 노드로 설정
cur = tmp # 현재 노드를 그 다음 노드로 변경
return prv
33 changes: 33 additions & 0 deletions set-matrix-zeroes/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# TC:O(n^2), SC:O(1)
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
first_row = False
first_col = False

#첫번째 행, 열 flag
for r in range(len(matrix)):
if matrix[r][0] == 0:
first_row = True
for c in range(len(matrix[0])):
if matrix[0][c] == 0:
first_col = True

#그 이외의 행, 열 flag
for r in range(1, len(matrix)):
for c in range(1, len(matrix[0])):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0

# 0으로 설정
for r in range(1, len(matrix)):
for c in range(1, len(matrix[0])):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0
# 첫번째 행과 열에 대해 각각 0으로 설정
if first_row:
for r in range(len(matrix)):
matrix[r][0] = 0
if first_col:
for c in range(len(matrix[0])):
matrix[0][c] = 0
12 changes: 12 additions & 0 deletions unique-paths/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TC:O(n*m), SC:O(1)
# 왼쪽아래로 가는 경로는 왼쪽으로 가는 경로의 경우의 수 + 아래로 가는 경로의 수
# 가장자리로 가는 경로의 경우의 수는 모두 1
# n크기의 리스트 값을 매 번 덮어써서 계산함
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
l = [1 for _ in range(n)]
for row in range(1, m):
for col in range(1, n):
l[col] += l[col - 1]

return l[-1]
Loading