Skip to content

Commit

Permalink
Merge pull request #526 from kjb512/main
Browse files Browse the repository at this point in the history
[kayden] Week 09 Solutions
  • Loading branch information
kjb512 authored Oct 12, 2024
2 parents e5c7d84 + 4a77a2f commit 359ae2c
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
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()
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)

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

0 comments on commit 359ae2c

Please sign in to comment.