diff --git a/coin-change/devyejin.py b/coin-change/devyejin.py new file mode 100644 index 000000000..f725a2fd6 --- /dev/null +++ b/coin-change/devyejin.py @@ -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 + diff --git a/find-minimum-in-rotated-sorted-array/devyejin.py b/find-minimum-in-rotated-sorted-array/devyejin.py new file mode 100644 index 000000000..4ff70cfd4 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/devyejin.py @@ -0,0 +1,3 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + return min(nums) diff --git a/maximum-depth-of-binary-tree/devyejin.py b/maximum-depth-of-binary-tree/devyejin.py new file mode 100644 index 000000000..7eecbb124 --- /dev/null +++ b/maximum-depth-of-binary-tree/devyejin.py @@ -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 +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 diff --git a/maximum-subarray/devyejin.py b/maximum-subarray/devyejin.py index 1d0ddeb82..b6003864c 100644 --- a/maximum-subarray/devyejin.py +++ b/maximum-subarray/devyejin.py @@ -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 diff --git a/merge-two-sorted-lists/devyejin.py b/merge-two-sorted-lists/devyejin.py new file mode 100644 index 000000000..b8e848ec1 --- /dev/null +++ b/merge-two-sorted-lists/devyejin.py @@ -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 diff --git a/word-search/devyejin.py b/word-search/devyejin.py new file mode 100644 index 000000000..e5f349cdb --- /dev/null +++ b/word-search/devyejin.py @@ -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 +