-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #844 from HodaeSsi/main
[HodaeSsi] Week4
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,17 @@ | ||
# space complexity: O(n) (여기서 n은 amount) | ||
# time complexity: O(n * m) (여기서 n은 amount, m은 coins의 길이) | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [float('inf')] * (amount + 1) | ||
dp[0] = 0 | ||
|
||
for i in range(1, amount + 1): | ||
for coin in coins: | ||
if coin <= i: | ||
dp[i] = min(dp[i], dp[i - coin] + 1) | ||
|
||
return dp[amount] if dp[amount] != float('inf') else -1 | ||
|
This file contains 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,22 @@ | ||
from typing import Optional | ||
|
||
|
||
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]: | ||
if not list1: | ||
return list2 | ||
if not list2: | ||
return list1 | ||
|
||
if list1.val < list2.val: | ||
list1.next = self.mergeTwoLists(list1.next, list2) | ||
return list1 | ||
else: | ||
list2.next = self.mergeTwoLists(list1, list2.next) | ||
return list2 | ||
|
This file contains 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,9 @@ | ||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
expected_sum = (n ** 2 + n) // 2 | ||
|
||
actual_sum = sum(nums) | ||
|
||
return expected_sum - actual_sum | ||
|
This file contains 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,19 @@ | ||
# space complexity: O(n^2) | ||
# time complexity: O(n^2) (*exactly, n^2 / 2) | ||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
dp = [[False] * len(s) for _ in range(len(s))] # dp[i][j] = True if s[i:j+1] is a palindrome | ||
for i in range(len(s)): | ||
for j in range(i, -1, -1): | ||
if i == j: | ||
dp[j][i] = True | ||
continue | ||
if i - j == 1: | ||
dp[j][i] = s[i] == s[j] | ||
continue | ||
if s[i] == s[j]: | ||
if dp[j+1][i-1]: | ||
dp[j][i] = True | ||
|
||
return sum([sum(row) for row in dp]) | ||
|
This file contains 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,29 @@ | ||
class Solution: | ||
def dfs (self, board, word, visited, y, x, word_idx): | ||
if word_idx == len(word): | ||
return True | ||
|
||
if y < 0 or y >= len(board) or x < 0 or x >= len(board[0]) or visited[y][x] or board[y][x] != word[word_idx]: | ||
return False | ||
|
||
visited[y][x] = True | ||
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]: | ||
if self.dfs(board, word, visited, y + dy, x + dx, word_idx + 1): | ||
return True | ||
visited[y][x] = False | ||
return False | ||
|
||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))] | ||
|
||
# find fisrt letter in board | ||
for y in range(len(board)): | ||
for x in range(len(board[0])): | ||
if board[y][x] == word[0]: | ||
visited[y][x] = True | ||
for dy, dx in [(1, 0), (-1, 0), (0, -1), (0, 1)]: | ||
if self.dfs(board, word, visited, y + dy, x + dx, 1): | ||
return True | ||
visited[y][x] = False | ||
return False | ||
|