Skip to content

Commit 8deb55d

Browse files
authored
Merge pull request #925 from jungsiroo/main
[jungsiroo] Week 7
2 parents a86ba5b + 58124b5 commit 8deb55d

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
"""
4+
딕셔너리를 사용하여 이전에 나왔던 인덱스를 기억
5+
중복되지 않았다면 answer를 저장해 나감
6+
중복이 발생하고 begin과 end사이라면 중복 발생 원인 다음을 begin으로 세팅 후 다시 계산
7+
8+
Time Complexity : O(n)
9+
Space Complexity : O(n)
10+
11+
"""
12+
answer, begin, end = 0, 0, 0
13+
hash_map = dict()
14+
15+
while end < len(s):
16+
ch = s[end]
17+
hash_map[ch] = hash_map.get(ch, -1)
18+
19+
if hash_map[ch] == -1: #한 번도 나오지 않았다.
20+
hash_map[ch] = end
21+
answer = max(answer, end-begin+1)
22+
else:
23+
if begin<=hash_map[ch]<end: #중복이 생겼다.
24+
begin = hash_map[ch]+1
25+
else:
26+
answer = max(answer, end-begin+1)
27+
hash_map[ch] = end
28+
end += 1
29+
30+
return answer
31+

number-of-islands/jungsiroo.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
# TC : O(n*m)
4+
# SC : O(n*m)
5+
6+
m, n = len(grid), len(grid[0])
7+
dx = [-1,1,0,0]
8+
dy = [0,0,-1,1]
9+
10+
def in_range(r, c):
11+
if r<0 or c<0 or r>=m or c>=n:
12+
return False
13+
return True
14+
15+
# DFS Way
16+
17+
def dfs(r, c):
18+
grid[r][c] = 'x'
19+
20+
for i in range(4):
21+
nr, nc = r+dx[i], c+dy[i]
22+
if not in_range(nr, nc) or grid[nr][nc] != '1':
23+
continue
24+
dfs(nr, nc)
25+
26+
# BFS Way
27+
from collections import deque
28+
def bfs(r, c):
29+
grid[r][c] = 'x'
30+
queue = deque()
31+
queue.append([r, c])
32+
33+
while queue:
34+
cr, cc = queue.popleft()
35+
for i in range(4):
36+
nr, nc = cr+dx[i], cc+dy[i]
37+
38+
if not in_range(nr, nc) or grid[nr][nc] != '1':
39+
continue
40+
grid[nr][nc] = 'x'
41+
queue.append([nr, nc])
42+
43+
ret = 0
44+
for i in range(m):
45+
for j in range(n):
46+
if grid[i][j] == '1':
47+
bfs(i, j)
48+
ret += 1
49+
return ret
50+

reverse-linked-list/jungsiroo.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3+
# iterative way
4+
5+
"""
6+
prev, curr = None, head
7+
while curr:
8+
tmp_nxt = curr.next
9+
10+
curr.next = prev
11+
prev, curr = curr, tmp_nxt
12+
13+
return prev
14+
"""
15+
16+
# Recursive Way
17+
if head is None or head.next is None:
18+
return head
19+
20+
new_head = self.reverseList(head.next)
21+
head.next.next = head # reversing pointer
22+
head.next = None
23+
return new_head
24+
25+
# 둘 다 시간복잡도 O(n)
26+
# 하지만 재귀의 경우 콜스택에 따른 공간복잡도 O(n)을 소요
27+
# iterative 방식은 O(1)
28+
29+

set-matrix-zeroes/jungsiroo.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
7+
"""
8+
# Naive change : save rows and cols
9+
# SC : O(m+n)
10+
row_zero, col_zero = set(), set()
11+
12+
rows, cols = len(matrix), len(matrix[0])
13+
for i in range(rows):
14+
for j in range(cols):
15+
if matrix[i][j] == 0:
16+
row_zero.add(i)
17+
col_zero.add(j)
18+
19+
for i in range(rows):
20+
for j in range(cols):
21+
if i in row_zero or j in col_zero:
22+
matrix[i][j] = 0
23+
"""
24+
25+
# Constant Space Complexity using bitmasking
26+
# 0인 구간을 toggle 시켜놓고 확인하는 방법
27+
def is_on(number, k):
28+
return (number & (1<<k)) != 0
29+
30+
row_zero, col_zero = 0, 0
31+
32+
rows, cols = len(matrix), len(matrix[0])
33+
for i in range(rows):
34+
for j in range(cols):
35+
if matrix[i][j] == 0:
36+
row_zero |= (1 << i)
37+
col_zero |= (1 << j)
38+
39+
for i in range(rows):
40+
for j in range(cols):
41+
if is_on(row_zero, i) or is_on(col_zero, j):
42+
matrix[i][j] = 0
43+

unique-paths/jungsiroo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from math import comb
2+
3+
class Solution:
4+
def uniquePaths(self, m: int, n: int) -> int:
5+
# mathematic way
6+
# 중학교 때 배운 최단거리 조합으로 구하기 문제
7+
# 문제 자체가 오른쪽, 아래밖에 못가기에 최단거리가 될 수 밖에 없음
8+
9+
"""
10+
TC : O(max(m, n))
11+
SC : O(1)
12+
"""
13+
14+
return comb(m+n-2, m-1)
15+

0 commit comments

Comments
 (0)