-
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
[jinah92] Week 4 #810
[jinah92] Week 4 #810
Changes from 2 commits
c1d6281
6489b9b
c981bfb
35cfa41
137592b
e1d03cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# O(C*A) times, O(A) spaces | ||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [0] + [amount + 1] * amount | ||
|
||
for coin in coins: | ||
for i in range(coin, amount + 1): | ||
dp[i] = min(dp[i], dp[i-coin]+1) | ||
|
||
return dp[amount] if dp[amount] < amount + 1 else -1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# O(m+n) times, O(1) spaces | ||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(None) | ||
node = dummy | ||
|
||
while list1 and list2: | ||
if list1.val < list2.val: | ||
node.next = list1 | ||
list1 = list1.next | ||
else: | ||
node.next = list2 | ||
list2 = list2.next | ||
|
||
node = node.next | ||
|
||
node.next = list1 or list2 | ||
return dummy.next |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# O(n) times, O(n) spaces | ||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nums_keys = dict.fromkeys(nums,0) | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
last = 0 | ||
|
||
for i in range(len(nums)): | ||
if not i in nums_keys: | ||
return i | ||
last += 1 | ||
|
||
return last |
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. 혹시 시간적 여유가 되신다면, 이 코드의 시간 복잡도를 줄여보는 것을 시도해보시면 어떨까요? 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. DP 풀이법 추가했습니다~ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# O((LogN)^N) times, O(1) spaces | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
sub_str_len = 1 | ||
result = 0 | ||
|
||
while sub_str_len <= len(s): | ||
start_idx = 0 | ||
while start_idx + sub_str_len <= len(s): | ||
sub_str = s[start_idx:start_idx+sub_str_len] | ||
if sub_str == sub_str[::-1]: | ||
result += 1 | ||
start_idx += 1 | ||
|
||
sub_str_len += 1 | ||
|
||
|
||
return result |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# O(M*N*4^W) times, O(M*N+W) spaces | ||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
rows, cols = len(board), len(board[0]) | ||
visited = set() | ||
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.
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. 방문여부를 별도 set으로 관리하지 않고, board에 빈 값을 넣고, 방문 이후에는 원래 값으로 원복하는 방법이 있을 것 같습니다. |
||
|
||
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] | ||
|
||
def dfs(row, col, idx): | ||
if idx == len(word): | ||
return True | ||
if not (0 <= row < rows and 0 <= col < cols): | ||
return False | ||
if board[row][col] != word[idx]: | ||
return False | ||
if (row, col) in visited: | ||
return False | ||
|
||
visited.add((row, col)) | ||
result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions) | ||
visited.remove((row, col)) | ||
|
||
return result | ||
|
||
return any(dfs(r, c, 0) for r in range(rows) for c in range(cols)) |
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.
이건 리뷰는 아니고, 제가 coin-change 문제를 풀면서 한가지 고민했던 부분을 공유드립니다. 문제의 예시는
coins
배열이 오름차순으로 주어지지만, 항상 오름차순으로 주어진다는 보장은 없습니다. 만약coins
가 오름차순으로 주어지지 않더라도 작성하신 코드가 잘작동할지 궁금합니다.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.
다음과 같은 이유로, dp 배열을 갱신하는데 영향을 끼치지 않기 때문에 coins 배열의 정렬 순서는 상관이 없을 것 같습니다