-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
Comment on lines
+6
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 문제의 경우 유명한게 Kadane 알고리즘도 있지만, 면접에서 divide and conquer로도 풀어보라는 경우가 많다고 하더라고요 참고해보시면 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여러 방법 알려주셔서 감사합니다! 해당 부분들도 공부해 보겠습니다! |
||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)으로 해결해보라는 방법도 고민해보시면 좋을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다! 공간복잡도를 줄이는 방법도 찾아봐야겠네요!