Skip to content
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

[kayden] Week 09 Solutions #526

Merged
merged 1 commit into from
Oct 12, 2024
Merged
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
14 changes: 14 additions & 0 deletions find-minimum-in-rotated-sorted-array/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
# 시간복잡도: O(logN)
# 공간복잡도: O(1)
def findMin(self, nums: List[int]) -> int:
n = len(nums)
st, en = 0, n-1
while st < en:
mid = (st+en)//2
if nums[mid] > nums[en]:
st = mid + 1
else:
en = mid

return nums[st]
14 changes: 14 additions & 0 deletions linked-list-cycle/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
# 시간복잡도: O(N)
# 공간복잡도: O(N)
def hasCycle(self, head: Optional[ListNode]) -> bool:

visited = set()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 방문한 node를 재방문하면 cycle이 존재한다는 풀이가 명확해서 좋은 것 같습니다. Follow up에 공간복잡도를 O(1)으로 해결해보라는 방법도 고민해보시면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다! 공간복잡도를 줄이는 방법도 찾아봐야겠네요!

while head:
if head in visited:
return True

visited.add(head)
head = head.next

return False
15 changes: 15 additions & 0 deletions maximum-subarray/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
# 시간복잡도: O(N)
# 공간복잡도: O(1)
def maxSubArray(self, nums: List[int]) -> int:

prev = 0
answer = float('-inf')
for num in nums:
if prev + num > num:
prev += num
else:
prev = num
answer = max(answer, prev)
Comment on lines +6 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제의 경우 유명한게 Kadane 알고리즘도 있지만, 면접에서 divide and conquer로도 풀어보라는 경우가 많다고 하더라고요 참고해보시면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러 방법 알려주셔서 감사합니다! 해당 부분들도 공부해 보겠습니다!


return max(prev, answer)
39 changes: 39 additions & 0 deletions minimum-window-substring/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import Counter, deque
class Solution:
# 시간복잡도: O(S+T)
# 공간복잡도: O(T)
def minWindow(self, s: str, t: str) -> str:
counter = Counter(t)

index = deque()
tot = 0
m = len(s)
st, en = 0, m - 1
for idx, ch in enumerate(s):
if ch not in counter:
continue

counter[ch] -= 1
index.append(idx)

if counter[ch] == 0:
tot += 1

while index:
if counter[s[index[0]]] < 0:
counter[s[index[0]]] += 1
index.popleft()
else:
break

if tot == len(counter):
a = index[0]
b = idx
if b - a + 1 < m:
st, en = a, b
m = en - st + 1

if tot != len(counter):
return ""

return s[st:en + 1]
57 changes: 57 additions & 0 deletions pacific-atlantic-water-flow/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution:
# 시간복잡도: O(N*M)
# 공간복잡도: O(N*M)
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:

m = len(heights)
n = len(heights[0])
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

dp = [[False] * (n) for _ in range(m)]
dp[0][n - 1] = True
dp[m - 1][0] = True
visited = set()

def dfs(x, y):
if dp[x][y]:
return 2

check = set()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]

if 0 <= nx < m and 0 <= ny < n:
if (nx, ny) not in visited and heights[nx][ny] <= heights[x][y]:
visited.add((nx, ny))
res = dfs(nx, ny)

if res != -1:
check.add(res)
visited.remove((nx, ny))
else:
if x == 0 or y == 0:
check.add(0)
if x == m - 1 or y == n - 1:
check.add(1)

if 2 in check:
dp[x][y] = 2
return 2

if len(check) == 2:
return 2

if check:
return check.pop()

return -1

answer = []
for i in range(m):
for j in range(n):
if dfs(i, j) == 2:
answer.append([i, j])

return answer