diff --git a/best-time-to-buy-and-sell-stock/8804who.py b/best-time-to-buy-and-sell-stock/8804who.py new file mode 100644 index 0000000000..a41ce83349 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/8804who.py @@ -0,0 +1,13 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + answer = 0 + + min_price = prices[0] + for price in prices: + if min_price>price: + min_price=price + + if answer int: + dp = [1e9] * (amount+1) + dp[0] = 0 + + for i in range(1, amount+1): + for coin in coins: + if i-coin>=0: + if dp[i-coin]+1 int: + left = 0 + right = len(nums)-1 + while True: + mid = (left+right)//2 + if nums[left] > nums[right]: + if nums[left] > nums[mid]: + right = mid + else: + left = mid+1 + else: + return nums[left] + diff --git a/group-anagrams/8804who.py b/group-anagrams/8804who.py new file mode 100644 index 0000000000..276beb2d15 --- /dev/null +++ b/group-anagrams/8804who.py @@ -0,0 +1,9 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + dic=defaultdict(list) + for string in strs: + sorted_str = ''.join(sorted(string)) + dic[sorted_str].append(string) + + return list(dic.values()) + diff --git a/maximum-depth-of-binary-tree/8804who.py b/maximum-depth-of-binary-tree/8804who.py new file mode 100644 index 0000000000..4b6ff51e7c --- /dev/null +++ b/maximum-depth-of-binary-tree/8804who.py @@ -0,0 +1,22 @@ +# 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 + + def dfs(node, depth): + l, r = depth, depth + + if node.left: + l = dfs(node.left, depth+1) + if node.right: + r = dfs(node.right, depth+1) + return max(l, r) + + return dfs(root, 1) + diff --git a/merge-two-sorted-lists/8804who.py b/merge-two-sorted-lists/8804who.py new file mode 100644 index 0000000000..32f50fcea4 --- /dev/null +++ b/merge-two-sorted-lists/8804who.py @@ -0,0 +1,36 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + sorted_list = [] + + while True: + if list1 is None and list2 is None: + break + elif list1 is None: + sorted_list.append(list2.val) + list2 = list2.next + elif list2 is None: + sorted_list.append(list1.val) + list1 = list1.next + else: + if list1.val > list2.val: + sorted_list.append(list2.val) + list2 = list2.next + else: + sorted_list.append(list1.val) + list1 = list1.next + + def get_node(idx): + if idx < len(sorted_list): + return ListNode(sorted_list[idx], get_node(idx+1)) + else: + return None + + answer= get_node(0) + + return answer + diff --git a/word-search/8804who.py b/word-search/8804who.py new file mode 100644 index 0000000000..2bc82a45f0 --- /dev/null +++ b/word-search/8804who.py @@ -0,0 +1,31 @@ +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + moves = [[-1, 0], [1, 0], [0, -1], [0, 1]] + max_y, max_x = len(board)-1, len(board[0])-1 + + visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))] + + + def dfs(y, x, idx): + if idx == len(word): + return True + for move in moves: + moved_y, moved_x = y+move[0], x+move[1] + if max_y >= moved_y and moved_y >= 0 and max_x >= moved_x and moved_x >= 0: + if board[moved_y][moved_x] == word[idx] and not visited[moved_y][moved_x]: + visited[moved_y][moved_x] = True + if dfs(moved_y, moved_x, idx+1): + return True + visited[moved_y][moved_x] = False + + + for y in range(max_y+1): + for x in range(max_x+1): + if board[y][x] == word[0]: + visited[y][x] = True + if dfs(y, x, 1): + return True + visited[y][x] = False + + return False +