diff --git a/coin-change/jinah92.py b/coin-change/jinah92.py new file mode 100644 index 000000000..b528841ea --- /dev/null +++ b/coin-change/jinah92.py @@ -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 diff --git a/merge-two-sorted-lists/jinah92.py b/merge-two-sorted-lists/jinah92.py new file mode 100644 index 000000000..d2fd2b872 --- /dev/null +++ b/merge-two-sorted-lists/jinah92.py @@ -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 diff --git a/missing-number/jinah92.py b/missing-number/jinah92.py new file mode 100644 index 000000000..357bb8070 --- /dev/null +++ b/missing-number/jinah92.py @@ -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) diff --git a/palindromic-substrings/jinah92.py b/palindromic-substrings/jinah92.py new file mode 100644 index 000000000..05e36bad3 --- /dev/null +++ b/palindromic-substrings/jinah92.py @@ -0,0 +1,39 @@ +# 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 + +# DP 풀이 +# O(N^2) times, O(N^2) spaces +# start, end 지점을 순회하면서 이전 계산값을 재사용하여 회문을 파악 +class Solution2: + def countSubstrings(self, s: str) -> int: + dp = {} + + for end in range(len(s)): + for start in range(end, -1, -1): + if start == end: + dp[(start, end)] = True + elif start + 1 == end: + dp[(start, end)] = s[start] == s[end] + else: + dp[(start, end)] = s[start] == s[end] and dp[(start+1, end-1)] + + return list(dp.values()).count(True) diff --git a/word-search/jinah92.py b/word-search/jinah92.py new file mode 100644 index 000000000..df3015a93 --- /dev/null +++ b/word-search/jinah92.py @@ -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))