Skip to content

Commit f09a6df

Browse files
authored
Merge pull request #489 from naringst/main
[나리] WEEK 07 Solutions
2 parents ec10a0d + cefbb6b commit f09a6df

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Runtime: 51ms, Memory: 16.83MB
3+
# Time complexity: O(len(s)^2)
4+
# Space complexity: O(len(s))
5+
6+
class Solution:
7+
def lengthOfLongestSubstring(self, s: str) -> int:
8+
stringArr = []
9+
maxLength = 0
10+
11+
for sub in s :
12+
if sub in stringArr :
13+
maxLength = max(maxLength, len(stringArr))
14+
repeatIdx = stringArr.index(sub)
15+
stringArr = stringArr[repeatIdx+1 :]
16+
17+
stringArr.append(sub)
18+
19+
maxLength = max(maxLength, len(stringArr))
20+
21+
22+
return maxLength

number-of-islands/naringst.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from collections import deque
2+
3+
# Runtime: 241ms, Memory: 18.94MB
4+
# Time complexity: O(len(n*m))
5+
# Space complexity: O(len(n*m))
6+
7+
8+
class Solution:
9+
def bfs(self, a,b, grid, visited) :
10+
n = len(grid)
11+
m = len(grid[0])
12+
13+
dx = [0, 0, 1, -1]
14+
dy = [1, -1 ,0 ,0]
15+
q = deque()
16+
q.append([a,b])
17+
visited[a][b] = True
18+
19+
while q :
20+
x,y = q.popleft()
21+
22+
for i in range(4) :
23+
nx = x + dx[i]
24+
ny = y + dy[i]
25+
26+
if (0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and grid[nx][ny] == '1'):
27+
visited[nx][ny] = True
28+
q.append([nx,ny])
29+
30+
31+
32+
def numIslands(self, grid: List[List[str]]) -> int:
33+
n = len(grid)
34+
m = len(grid[0])
35+
visited = [[False] * m for _ in range(n)]
36+
answer = 0
37+
38+
for i in range(n) :
39+
for j in range(m) :
40+
if grid[i][j] == '1' and not visited[i][j] :
41+
self.bfs(i,j,grid,visited)
42+
answer += 1
43+
44+
return answer

reverse-linked-list/naringst.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# Runtime: 39ms, Memory: 17.88MB
3+
# Time complexity: O(len(head))
4+
# Space complexity: O(len(head))
5+
6+
7+
# Definition for singly-linked list.
8+
# class ListNode:
9+
# def __init__(self, val=0, next=None):
10+
# self.val = val
11+
# self.next = next
12+
13+
class Solution:
14+
def __init__(self):
15+
self.nodes = [] # ListNode 객체를 저장할 배열
16+
17+
def reverseList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]:
18+
prev = None
19+
curr = head
20+
21+
while curr is not None:
22+
nextNode = curr.next
23+
curr.next = prev
24+
prev = curr
25+
curr = nextNode
26+
27+
return prev

0 commit comments

Comments
 (0)