Skip to content
Open
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
17 changes: 17 additions & 0 deletions merge-intervals/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# idea : -
# TimeComplexity: # O(n log(n))
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals = sorted(intervals)
output = [intervals[0]]

# Merge when intervals overlap
for start, end in intervals[1:]:
lastEnd = output[-1][1]
if start <= lastEnd:
output[-1][1] = max(lastEnd, end)
else:
output.append([start, end])
return output


12 changes: 12 additions & 0 deletions missing-number/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# idea : -
# Time Complexity : below O(n^2)

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)


43 changes: 43 additions & 0 deletions reorder-list/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# idea: Two-Pointer
# Time Complexixty: O(n)?
'''
Linked lists do not provide a len func, split at the middle and merge the two halves sequentially
'''

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head):
if not head or not head.next:
return

# 1. middle
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next

# 2. reverse second half
prev = None
cur = slow.next
slow.next = None

while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt

# 3. merge
first, second = head, prev
while second:
t1, t2 = first.next, second.next
first.next = second
second.next = t1
first = t1
second = t2