Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 19 additions & 0 deletions find-minimum-in-rotated-sorted-array/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
# You must write an algorithm that runs in O(log n) time. >> Binary Search
# It's possible because the array is sorted.
left, right = 0, len(nums)-1

while left < right:
mid = (left + right) // 2
# If the value at mid is greater than the value at the right end, it means the minimum place to the right of mid.
if nums[mid] > nums[right]:
left = mid + 1
else:
# mid can be minimum number
# But is it possible to solve it with "right=mid-1"? e.g,[4,5,6,7,0,1,2]
right = mid
return nums[left]



36 changes: 36 additions & 0 deletions maximum-depth-of-binary-tree/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# idea : DFS

# time : O(n)
# space : O(n)

class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
# edge case
if not root:
return 0
max_depth = 0
stack = [(root, 1)]
while stack:
node, depth = stack.pop()
max_depth = max(depth, max_depth)
if node.left:
stack.append((node.left, depth+1))
if node.right:
stack.append((node.right, depth+1))
return max_depth




# another way : Down-top : recursive
# class Solution:
# def maxDepth(self, root: Optional[TreeNode]) -> int:
# if not root:
# return 0
# return 1 + max(
# self.maxDepth(root.left),
# self.maxDepth(root.right)
# )



31 changes: 31 additions & 0 deletions merge-two-sorted-lists/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# idea : Two Pointer
# It was first time solving a two-pointer problem using nodes instead of arrays, and I realized I'm still not very familiar with nodes yet.
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


# Another way to solve it
# class Solution:
# def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# if not (list1 and list2):
# return list1 or list2
# if list1.val < list2.val:
# list1.next = self.mergeTwoLists(list1.next, list2)
# return list1
# else:
# list2.next = self.mergeTwoLists(list1, list2.next)
# return list2