Skip to content

Commit

Permalink
Merge pull request #806 from KwonNayeon/main
Browse files Browse the repository at this point in the history
[KwonNayeon] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 4acfa7b + b13a229 commit 27ece99
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
30 changes: 30 additions & 0 deletions coin-change/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Constraints:
1. 1 <= coins.length <= 12
2. 1 <= coins[i] <= 2^31 - 1
3. 0 <= amount <= 10^4
Time Complexity: O(N*M)
- N์€ amount
- M์€ ๋™์ „์˜ ๊ฐœ์ˆ˜ (coins.length)
Space Complexity: O(N)
- N์€ amount
To Do:
- DP ๋ฌธ์ œ ๋ณต์Šตํ•˜๊ธฐ
- Bottom-up๊ณผ Top-down ๋ฐฉ์‹์˜ ์ฐจ์ด์  ์ดํ•ดํ•˜๊ธฐ
- ๊ทธ๋ฆฌ๋””์™€ DP์˜ ์ฐจ์ด์  ๋ณต์Šตํ•˜๊ธฐ
"""

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [sys.maxsize // 2] * (amount + 1)
dp[0] = 0

for i in range(1, amount + 1):
for coin in coins:
if i >= coin:
dp[i] = min(dp[i], dp[i-coin] + 1)

return dp[amount] if dp[amount] != sys.maxsize // 2 else -1
45 changes: 45 additions & 0 deletions merge-two-sorted-lists/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Constraints:
1. 0 <= list1.length, list2.length <= 50
2. -100 <= Node.val <= 100
3. list1 and list2 are sorted in non-decreasing order
Time Complexity: n๊ณผ m์ด ๊ฐ๊ฐ list1๊ณผ list2์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ, O(n + m)
- ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋ฐฉ๋ฌธํ•˜๊ธฐ ๋•Œ๋ฌธ
Space Complexity: O(1)
- ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋…ธ๋“œ๋“ค์˜ ์—ฐ๊ฒฐ๋งŒ ๋ฐ”๊พธ๊ธฐ ๋•Œ๋ฌธ
ํ’€์ด ๋ฐฉ๋ฒ•:
1. ๋”๋ฏธ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉ
2. ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ’์„ ๋น„๊ต
3. ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์—ฐ๊ฒฐํ•˜๊ณ , ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์˜ ํฌ์ธํ„ฐ๋ฅผ ๋‹ค์Œ์œผ๋กœ ์ด๋™
4. ํ•œ์ชฝ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚˜๋ฉด, ๋‚จ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ ๋’ค์— ์—ฐ๊ฒฐ
5. ๋”๋ฏธ ๋…ธ๋“œ์˜ next๋ฅผ ๋ฐ˜ํ™˜ (์‹ค์ œ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ )
"""

# 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]:
result = ListNode()
current = result

while list1 and list2:
if list1.val <= list2.val:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next

if list1:
current.next = list1
if list2:
current.next = list2

return result.next
28 changes: 28 additions & 0 deletions missing-number/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Constraints:
1. n equals the length of array nums
2. n is between 1 and 10^4 inclusive
3. Each element nums[i] is between 0 and n inclusive
4. All numbers in nums are unique (no duplicates)
Time Complexity: O(nlogn)
- ์ •๋ ฌ์— nlogn, ์ˆœํšŒ์— n์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์ „์ฒด์ ์œผ๋กœ O(nlogn)
Space Complexity: O(1)
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ž…๋ ฅ ๋ฐฐ์—ด๋งŒ ์‚ฌ์šฉ
ํ’€์ด ๋ฐฉ๋ฒ•:
1. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜์—ฌ 0๋ถ€ํ„ฐ n๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์žˆ์–ด์•ผ ํ•  ์œ„์น˜์— ์—†๋Š” ์ˆซ์ž๋ฅผ ์ฐพ์Œ
2. ์ธ๋ฑ์Šค์™€ ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ๋‹ค๋ฅด๋‹ค๋ฉด ๊ทธ ์ธ๋ฑ์Šค๊ฐ€ missing number
3. ๋ชจ๋“  ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ–ˆ๋Š”๋ฐ๋„ ์—†๋‹ค๋ฉด n์ด missing number
"""

class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums.sort()

for i in range(len(nums)):
if nums[i] != i:
return i

return len(nums)

51 changes: 51 additions & 0 deletions word-search/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Constraints:
1. m equals board length (number of rows)
2. n equals board[i] length (number of columns)
3. m and n are between 1 and 6 inclusive
4. word length is between 1 and 15 inclusive
5. board and word contain only lowercase and uppercase English letters
Time Complexity: O(N * 3^L)
- N์€ board์˜ ๋ชจ๋“  cell (m * n)
- L์€ word์˜ ๊ธธ์ด
- ๊ฐ cell์—์„œ ์‹œ์ž‘ํ•˜์—ฌ word์˜ ๊ฐ ๊ธ€์ž๋งˆ๋‹ค ์„ธ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ (์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋ฐฉํ–ฅ ์ œ์™ธ)
Space Complexity: O(L)
- L์€ word์˜ ๊ธธ์ด๋กœ, ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด
To Do:
- DFS์™€ ๋ฐฑํŠธ๋ž˜ํ‚น ๊ฐœ๋… ๋ณต์Šตํ•˜๊ธฐ
- ๋‹ค๋ฅธ ์Šคํ„ฐ๋””์›๋ถ„๋“ค์˜ ๋‹ต์•ˆ ์ฐธ์กฐํ•˜์—ฌ ๋‹ค๋ฅธ ํ’€์ด๋ฐฉ๋ฒ• ๋ณต์Šตํ•˜๊ธฐ
"""

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])

def dfs(i, j, k):
if k == len(word):
return True

if (i < 0 or i >= rows or
j < 0 or j >= cols or
board[i][j] != word[k]):
return False

temp = board[i][j]
board[i][j] = '#'

result = (dfs(i+1, j, k+1) or
dfs(i-1, j, k+1) or
dfs(i, j+1, k+1) or
dfs(i, j-1, k+1))

board[i][j] = temp
return result

for i in range(rows):
for j in range(cols):
if board[i][j] == word[0]:
if dfs(i, j, 0):
return True
return False

0 comments on commit 27ece99

Please sign in to comment.