Skip to content

Commit 72015a4

Browse files
authored
Merge pull request #487 from kjb512/main
[kayden] Week 07 Solutions
2 parents ae84911 + 00458c7 commit 72015a4

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# 시간복잡도: O(N)
3+
# 공간복잡도: O(N)
4+
# set에서 제외하는 로직을 없애기 위해, map을 사용해서 idx값을 저장후, start와 비교해서 start보다 작은 idx를 가진 경우에는 중복이 아니라고 판단했습니다.
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
7+
last_idx = {}
8+
answer = 0
9+
start = 0
10+
11+
for idx, ch in enumerate(s):
12+
# 중복 조회시 idx값과 start 값 비교를 통해, 삭제하는 로직을 없이 중복을 확인했습니다.
13+
if ch in last_idx and last_idx[ch] >= start:
14+
start = last_idx[ch] + 1
15+
last_idx[ch] = idx
16+
else:
17+
answer = max(answer, idx - start + 1)
18+
last_idx[ch] = idx
19+
20+
return answer

number-of-islands/kayden.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 시간복잡도: O(M*N)
2+
# 공간복잡도: O(M*N)
3+
4+
from collections import deque
5+
6+
7+
class Solution:
8+
def numIslands(self, grid: List[List[str]]) -> int:
9+
dx = [0, 0, -1, 1]
10+
dy = [-1, 1, 0, 0]
11+
m = len(grid)
12+
n = len(grid[0])
13+
q = deque()
14+
15+
def bfs(a, b):
16+
q.append((a, b))
17+
while q:
18+
x, y = q.popleft()
19+
20+
for i in range(4):
21+
nx = x + dx[i]
22+
ny = y + dy[i]
23+
if not (0 <= nx < m and 0 <= ny < n): continue
24+
25+
if grid[nx][ny] == '1':
26+
grid[nx][ny] = '0'
27+
q.append((nx, ny))
28+
29+
count = 0
30+
for i in range(m):
31+
for j in range(n):
32+
if grid[i][j] == '1':
33+
count += 1
34+
bfs(i, j)
35+
36+
return count

reverse-linked-list/kayden.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
// 시간복잡도: O(N)
12+
// 공간복잡도: O(1)
13+
class Solution {
14+
public ListNode reverseList(ListNode head) {
15+
ListNode prev = null;
16+
17+
while (head != null){
18+
ListNode next = head.next;
19+
head.next = prev;
20+
prev = head;
21+
head = next;
22+
}
23+
24+
return prev;
25+
}
26+
}

set-matrix-zeroes/kayden.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 시간복잡도: O(m*n)
2+
# 공간복잡도: O(m+n)
3+
class Solution:
4+
def setZeroes(self, matrix: List[List[int]]) -> None:
5+
m = len(matrix)
6+
n = len(matrix[0])
7+
8+
rows = set()
9+
cols = set()
10+
11+
for i in range(m):
12+
for j in range(n):
13+
if matrix[i][j] == 0:
14+
rows.add(i)
15+
cols.add(j)
16+
17+
for row in rows:
18+
for j in range(n):
19+
matrix[row][j] = 0
20+
21+
for col in cols:
22+
for i in range(m):
23+
matrix[i][col] = 0

unique-paths/kayden.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from math import comb
2+
class Solution:
3+
# 시간복잡도: O(m+n)
4+
# 공간복잡도: O(1)
5+
def uniquePaths(self, m: int, n: int) -> int:
6+
return comb(m+n-2, n-1) # m+n-2Cn-1
7+
8+
# 시간복잡도: O(m*n)
9+
# 공간복잡도: O(n)
10+
def uniquePaths2(self, m: int, n: int) -> int:
11+
dp = [1] * n
12+
13+
for _ in range(1, m):
14+
for j in range(1, n):
15+
dp[j] = dp[j-1] + dp[j]
16+
17+
return dp[n-1]

0 commit comments

Comments
 (0)