-
-
Notifications
You must be signed in to change notification settings - Fork 247
[devyejin] WEEK 04 solutions #1824
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
855e2fc
maximum depth of binary tree solution
devyejin 7fd579d
maxDepth 계산 단순화
devyejin 3bdc80f
merge two sorted lists solution
devyejin 065acb2
maximum subarray 로직 수정 : 공간 최적화
devyejin 4bd55ba
word search solution
devyejin 80955d9
find minimum in rotated sorted array solution
devyejin 56cd324
줄바꿈 추가
devyejin 08cfbf7
coin change solution
devyejin f93ae6c
fix : 타입 일관성 유지, ListNode 생성 시 None 대신 기본값 사용
devyejin 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 hidden or 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,46 @@ | ||
# # 중복조합 | ||
# class Solution: | ||
# def coinChange(self, coins: List[int], amount: int) -> int: | ||
# | ||
# def backtrack(current, total): | ||
# if total == amount: | ||
# return len(current) | ||
# | ||
# if total > amount: | ||
# return float('inf') | ||
# | ||
# min_count = float('inf') | ||
# for coin in coins: | ||
# current.append(coin) | ||
# result = backtrack(current, total + coin) | ||
# min_count = min(min_count, result) | ||
# current.pop() | ||
# | ||
# return min_count | ||
# | ||
# ans = backtrack([], 0) | ||
# return -1 if ans == float('inf') else ans | ||
from collections import deque | ||
from typing import List | ||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
if amount == 0: | ||
return 0 | ||
|
||
queue = deque([(0, 0)]) | ||
visited = set([0]) | ||
|
||
while queue: | ||
current_amount, count = queue.popleft() | ||
|
||
for coin in coins: | ||
new_amount = current_amount + coin | ||
if new_amount == amount: | ||
return count + 1 | ||
if new_amount < amount and new_amount not in visited: | ||
visited.add(new_amount) | ||
queue.append((new_amount, count + 1)) | ||
|
||
return -1 | ||
|
This file contains hidden or 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,3 @@ | ||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
return min(nums) |
This file contains hidden or 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,12 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
devyejin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
if not root: | ||
return 0 | ||
|
||
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | ||
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. 맞아요! 조언 감사합니다~ 앞으로 꾸준히 작성해볼게요 🥰 |
This file contains hidden or 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
class Solution: | ||
def maxSubArray(self, nums: list[int]) -> int: | ||
dp = [0] * len(nums) | ||
dp[0] = nums[0] | ||
current_sum = nums[0] | ||
max_sum = nums[0] | ||
|
||
for i in range(1, len(nums)): | ||
dp[i] = max(nums[i], dp[i - 1] + nums[i]) | ||
current_sum = max(nums[i], current_sum + nums[i]) | ||
max_sum = max(current_sum, max_sum) | ||
|
||
return max(dp) | ||
return max_sum | ||
|
This file contains hidden or 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,30 @@ | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
""" | ||
time : O(m+n) | ||
space : O(1) | ||
""" | ||
from typing import Optional | ||
|
||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode() | ||
|
||
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 hidden or 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,31 @@ | ||
class Solution: | ||
def exist(self, board: list[list[str]], word: str) -> bool: | ||
def backtrack(x, y, idx): | ||
if idx == len(word): | ||
return True | ||
|
||
if not (0 <= x < m and 0 <= y < n) or board[x][y] != word[idx]: | ||
return False | ||
|
||
tmp, board[x][y] = board[x][y], "@" | ||
|
||
res = ( | ||
backtrack(x + 1, y, idx + 1) or | ||
backtrack(x - 1, y, idx + 1) or | ||
backtrack(x, y + 1, idx + 1) or | ||
backtrack(x, y - 1, idx + 1) | ||
) | ||
|
||
board[x][y] = tmp | ||
return res | ||
|
||
m = len(board) | ||
n = len(board[0]) | ||
|
||
for r in range(m): | ||
for c in range(n): | ||
if backtrack(r, c, 0): | ||
return True | ||
|
||
return False | ||
|
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.
깊은 트리에서는 Python recursion limit 문제 발생 가능 → BFS나 stack 기반 DFS 방식도 고려하면 좋음.
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.
습관적으로 DFS 쓰게되는데 다른 방식 추천 감사합니다!