Skip to content
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
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
answer = 0

min_price = prices[0]
for price in prices:
if min_price>price:
min_price=price

if answer<price-min_price:
answer=price-min_price
return answer

13 changes: 13 additions & 0 deletions coin-change/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [1e9] * (amount+1)
dp[0] = 0

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

return -1 if dp[amount]==1e9 else dp[amount]

14 changes: 14 additions & 0 deletions find-minimum-in-rotated-sorted-array/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums)-1
while True:
Copy link
Contributor

Choose a reason for hiding this comment

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

지금은 로직상 문제가 없어서 잘 진행되지만, 혹시나 추후에 실수할 것을 대비해서
while left < right와 같은 방식으로 작성하면 더 안정적일 것 같습니다!

mid = (left+right)//2
if nums[left] > nums[right]:
if nums[left] > nums[mid]:
right = mid
else:
left = mid+1
else:
return nums[left]

9 changes: 9 additions & 0 deletions group-anagrams/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
dic=defaultdict(list)
for string in strs:
sorted_str = ''.join(sorted(string))
dic[sorted_str].append(string)

return list(dic.values())

22 changes: 22 additions & 0 deletions maximum-depth-of-binary-tree/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0

def dfs(node, depth):
l, r = depth, depth

if node.left:
l = dfs(node.left, depth+1)
if node.right:
r = dfs(node.right, depth+1)
return max(l, r)

return dfs(root, 1)

36 changes: 36 additions & 0 deletions merge-two-sorted-lists/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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]:
sorted_list = []

while True:
if list1 is None and list2 is None:
break
elif list1 is None:
sorted_list.append(list2.val)
list2 = list2.next
elif list2 is None:
sorted_list.append(list1.val)
list1 = list1.next
else:
if list1.val > list2.val:
sorted_list.append(list2.val)
list2 = list2.next
else:
sorted_list.append(list1.val)
list1 = list1.next

def get_node(idx):
if idx < len(sorted_list):
return ListNode(sorted_list[idx], get_node(idx+1))
else:
return None

answer= get_node(0)

return answer

31 changes: 31 additions & 0 deletions word-search/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
moves = [[-1, 0], [1, 0], [0, -1], [0, 1]]
max_y, max_x = len(board)-1, len(board[0])-1

visited = [[False for _ in range(len(board[0]))] for _ in range(len(board))]


def dfs(y, x, idx):
if idx == len(word):
return True
for move in moves:
moved_y, moved_x = y+move[0], x+move[1]
if max_y >= moved_y and moved_y >= 0 and max_x >= moved_x and moved_x >= 0:
if board[moved_y][moved_x] == word[idx] and not visited[moved_y][moved_x]:
visited[moved_y][moved_x] = True
if dfs(moved_y, moved_x, idx+1):
return True
visited[moved_y][moved_x] = False


for y in range(max_y+1):
for x in range(max_x+1):
if board[y][x] == word[0]:
visited[y][x] = True
if dfs(y, x, 1):
return True
visited[y][x] = False

return False