-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #526 from kjb512/main
[kayden] Week 09 Solutions
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |