diff --git a/coin-change/sangbeenmoon.py b/coin-change/sangbeenmoon.py index ac0885863e..0ceb5492e8 100644 --- a/coin-change/sangbeenmoon.py +++ b/coin-change/sangbeenmoon.py @@ -30,3 +30,44 @@ def go(self, x: int, coins: List[int]) -> int: self.dp[x] = mm if mm != 100000 else -1 # memoization return self.dp[x] + + + + + + + + +# -------- + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + memo = {} + + + for coin in coins: + memo[coin] = 1 + + def go(target: int) -> int: + if target < 0: + return -1 + + if target in memo: + return memo[target] + + min_val = 10001 + + for coin in coins: + res = go(target - coin) + if res >= 0: + min_val = min(min_val, res) + + memo[target] = (min_val + 1 if min_val != 10001 else -1) + + return memo[target] + + if amount == 0: + return 0 + + return go(amount) + diff --git a/find-minimum-in-rotated-sorted-array/sangbeenmoon.py b/find-minimum-in-rotated-sorted-array/sangbeenmoon.py index 38fc422ed9..6638b015cf 100644 --- a/find-minimum-in-rotated-sorted-array/sangbeenmoon.py +++ b/find-minimum-in-rotated-sorted-array/sangbeenmoon.py @@ -20,3 +20,25 @@ def findMin(self, nums: List[int]) -> int: right = mid return nums[left] + +# ----------- + +# TC : O(logN) +# SC : 1 + +class Solution: + def findMin(self, nums: List[int]) -> int: + + left = 0 + right = len(nums) - 1 + mid = (left + right) // 2 + + while left < right: + mid = (left + right) // 2 + + if nums[mid] > nums[right]: + left = mid + 1 + else: + right = mid + + return nums[left] diff --git a/word-search/sangbeenmoon.py b/word-search/sangbeenmoon.py index c75805843b..5cfbf722f7 100644 --- a/word-search/sangbeenmoon.py +++ b/word-search/sangbeenmoon.py @@ -34,3 +34,43 @@ def dfs(self, board, word, cx, cy, idx): self.visited[ny][nx] = True self.dfs(board, word, nx, ny, idx + 1) self.visited[ny][nx] = False + + +# --- + +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + + dx = [0,0,-1,1] + dy = [-1,1,0,0] + + x_len , y_len = len(board[0]) , len(board) + + visited = [[False] * x_len for _ in range(y_len)] + + def go(xx: int, yy: int, pos: int) -> bool: + + if pos >= len(word) - 1: + return True + + for d in range(4): + nx = xx + dx[d] + ny = yy + dy[d] + + if 0 <= nx and nx < x_len and 0 <= ny and ny < y_len: + if not visited[ny][nx] and board[ny][nx] == word[pos + 1]: + visited[ny][nx] = True + if go(nx,ny,pos+1): + return True + visited[ny][nx] = False + return False + + for y in range(y_len): + for x in range(x_len): + if board[y][x] == word[0]: + visited[y][x] = True + if go(x,y,0): + return True + visited[y][x] = False + + return False