-
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
Merged
Merged
[jinah92] Week 4 #810
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c1d6281
feat: week4 easy 문제 풀이
jinah92 6489b9b
feat: week4 medium 문제풀이
jinah92 c981bfb
feat: missing-number 알고리즘 리팩토링
jinah92 35cfa41
chore: 시간복잡도 수정 및 설명 추가
jinah92 137592b
feat: word-search 알고리즘 리팩토링
jinah92 e1d03cd
feat: Palindromic Substrings 알고리즘 개선
jinah92 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,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 |
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,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 |
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,10 @@ | ||
# O(n) times, O(n) spaces | ||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
nums_keys = set(nums) | ||
|
||
for i in range(len(nums)): | ||
if not i in nums_keys: | ||
return i | ||
|
||
return len(nums) |
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 풀이법 추가했습니다~ |
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,21 @@ | ||
# O(N^3) times, O(1) spaces | ||
# 내부 while문의 관계가 외부 while문의 sub_str_len에 따라 반복횟수가 줄어드므로, 1+2+...N = N(N-1)/2 = O(N2) 시간 소요 | ||
# 추라고 내부 while에서 sub_str_len에 따라 s가 인덱싱되므로 최대 O(N) 시간이 소요 | ||
# 최종적으로 O(N^2 * N) = O(N^3)이 소요됨 | ||
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 |
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,24 @@ | ||
# O(M*N*4^W) times, O(W) spaces | ||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
rows, cols = len(board), len(board[0]) | ||
|
||
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 | ||
|
||
temp_val = board[row][col] | ||
board[row][col] = "" | ||
|
||
result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions) | ||
|
||
board[row][col] = temp_val | ||
return result | ||
|
||
return any(dfs(r, c, 0) for r in range(rows) for c in range(cols)) |
Oops, something went wrong.
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.
이건 리뷰는 아니고, 제가 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 배열의 정렬 순서는 상관이 없을 것 같습니다