Skip to content

[i-mprovising] Week 07 solutions #1459

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions longest-substring-without-repeating-characters/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from collections import deque

class Solution:
"""
Time complexity O(n)
Space complexity O(n)
"""
def lengthOfLongestSubstring(self, s: str) -> int:
max_len = 0
q = deque()
for i, ch in enumerate(s):
if ch not in q:
q.append(ch)
if len(q) > max_len:
max_len = len(q)
else:
while True:
tmp = q.popleft()
if tmp == ch:
break
q.append(ch)

return max_len

def slidingWindow(self, s):
start, end = 0, 0
substr = set()
max_len = 0
while end < len(s):
if s[end] in substr:
substr.remove(s[start])
start += 1
else:
substr.add(s[end])
end += 1
max_len = max(end - start, max_len)
return max_len
32 changes: 32 additions & 0 deletions number-of-islands/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
"""
Time, Space comlexity O(n*m)

connected components
dfs, bfs
"""
def numIslands(self, grid: List[List[str]]) -> int:
n, m = len(grid), len(grid[0])
visited = [[False for _ in range(m)] for _ in range(n)] # visited 대신 grid를 0으로 표시할수도 있다
islands = 0

def dfs(x, y):
stack = [(x, y)]
while stack:
x, y = stack.pop()
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for k in range(4):
nx, ny = x + dx[k], y + dy[k]
if 0<=nx<=n-1 and 0<=ny<=m-1:
if not visited[nx][ny] and grid[nx][ny] == "1":
visited[nx][ny] = True
stack.append((nx, ny))

for i in range(n):
for j in range(m):
if not visited[i][j] and grid[i][j] == "1":
dfs(i, j)
islands += 1

return islands
25 changes: 25 additions & 0 deletions reverse-linked-list/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
"""
Time complexity O(n)
Space complexity O(1)
"""
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head

node = head.next
prev = head
prev.next = None

while node:
next_node = node.next
node.next = prev
prev = node
node = next_node

return prev
23 changes: 23 additions & 0 deletions set-matrix-zeroes/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Time, space complexity O(m * n)
"""

class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
m, n = len(matrix), len(matrix[0])
i_indices = set()
j_indices = set()
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
i_indices.add(i)
j_indices.add(j)

for i in i_indices:
matrix[i] = [0 for _ in range(n)]
for j in j_indices:
for i in range(m):
matrix[i][j] = 0
21 changes: 21 additions & 0 deletions unique-paths/i-mprovising.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Time, space complexity O(m*n)

Dynamic programming
"""

class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp = [[0 for _ in range(n)] for _ in range(m)]
dp[0] = [1 for _ in range(n)]

for i in range(1, m):
for j in range(n):
if i == 0:
continue
if j == 0:
dp[i][j] = 1
continue
dp[i][j] = dp[i-1][j] + dp[i][j-1]

return dp[-1][-1]