Skip to content

Commit 5cd9818

Browse files
authored
Merge pull request #1462 from yyyyyyyyyKim/main
[yyyyyyyyyKim] WEEK 07 solutions
2 parents 1d6ee79 + 629c00c commit 5cd9818

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
4+
# 브루트포스(시간복잡도 : O(n^2))
5+
answer = 0
6+
7+
for i in range(len(s)):
8+
# 중복없는 문자열을 저장할 집합
9+
substring = set()
10+
11+
for j in range(i,len(s)):
12+
13+
# 중복 문자를 만나면 break
14+
if s[j] in substring:
15+
break
16+
17+
# 중복 아니면 문자 추가하고 긴 문자열 길이 비교해서 업데이트
18+
substring.add(s[j])
19+
answer = max(answer, len(substring))
20+
21+
return answer

number-of-islands/yyyyyyyyyKim.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
4+
# DFS (시간복잡도 O(m*n), 공간복잡도 O(m*n))
5+
answer = 0 # 섬의 수
6+
m = len(grid)
7+
n = len(grid[0])
8+
9+
# 하나의 섬 처리(연결된 땅 모두 방문)
10+
def dfs(x,y):
11+
# 범위를 벗어나거나, 이미 방문했거나, 땅이 아니면 종료
12+
if x < 0 or y < 0 or x >= m or y >= n or grid[x][y] != "1":
13+
return
14+
15+
# 현재 땅 방문처리
16+
grid[x][y] = "*"
17+
18+
# 상하좌우 탐색
19+
dfs(x+1, y)
20+
dfs(x-1, y)
21+
dfs(x, y+1)
22+
dfs(x, y-1)
23+
24+
for i in range(m):
25+
for j in range(n):
26+
# 땅 발견시 dfs로 연결되어 있는 모든 땅 방문하고 섬+1 처리
27+
if grid[i][j] == "1":
28+
dfs(i,j)
29+
answer += 1
30+
31+
return answer

reverse-linked-list/yyyyyyyyyKim.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
9+
# 시간복잡도 O(n), 공간복잡도 O(1)
10+
answer = None
11+
12+
while head:
13+
next_node = head.next # 다음 노드 저장
14+
head.next = answer # 현재 노드의 next를 이전 노드로 변경
15+
answer = head # answer를 현재 노드로 업데이트
16+
head = next_node # head를 다음 노드로 이동
17+
18+
# answer = 역순 리스트의 head
19+
return answer

set-matrix-zeroes/yyyyyyyyyKim.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def setZeroes(self, matrix: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify matrix in-place instead.
5+
"""
6+
# 0이 있는 행, 열을 기록해두고 그 기록을 토대로 바꾸기(시간복잡도 O(m*n), 공간복잡도 O(m+n))
7+
# Follow up : 공간복잡도 O(1) 생각해볼것
8+
m, n = len(matrix), len(matrix[0])
9+
# rows, cols = [] , [] -> 중복되어 들어갈 수 있음. 불필요함. 중복제거를 위해 set 사용.
10+
rows, cols = set(), set()
11+
12+
# 0이 있는 행, 열 기록
13+
for i in range(m):
14+
for j in range(n):
15+
if matrix[i][j] == 0:
16+
rows.add(i)
17+
cols.add(j)
18+
19+
# 기록된 행 0으로 바꾸기
20+
for i in rows:
21+
for j in range(n):
22+
matrix[i][j] = 0
23+
24+
# 기록된 열 0으로 바꾸기
25+
for j in cols:
26+
for i in range(m):
27+
matrix[i][j] = 0

unique-paths/yyyyyyyyyKim.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def uniquePaths(self, m: int, n: int) -> int:
3+
4+
# DP (시간복잡도 O(m*n), 공간복잡도 O(m*n))
5+
# 모든 1행과 1열은 경로가 1개이므로 1로 배열 초기화.
6+
dp = [[1]*n for _ in range(m)]
7+
8+
for i in range(1,m):
9+
for j in range(1,n):
10+
# 현재위치 경로 경우의 수 = 위쪽 + 왼쪽
11+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
12+
13+
return dp[m-1][n-1]

0 commit comments

Comments
 (0)