Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jinah92] Week 4 #810

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions coin-change/jinah92.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions merge-two-sorted-lists/jinah92.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions missing-number/jinah92.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions palindromic-substrings/jinah92.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 시간적 여유가 되신다면, 이 코드의 시간 복잡도를 줄여보는 것을 시도해보시면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP 풀이법 추가했습니다~

Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions word-search/jinah92.py
Original file line number Diff line number Diff line change
@@ -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))
Loading