diff --git a/merge-intervals/ppxyn1.py b/merge-intervals/ppxyn1.py new file mode 100644 index 0000000000..5d169b48f6 --- /dev/null +++ b/merge-intervals/ppxyn1.py @@ -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 + + diff --git a/missing-number/ppxyn1.py b/missing-number/ppxyn1.py new file mode 100644 index 0000000000..7675528a70 --- /dev/null +++ b/missing-number/ppxyn1.py @@ -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) + + diff --git a/reorder-list/ppxyn1.py b/reorder-list/ppxyn1.py new file mode 100644 index 0000000000..3fc6702244 --- /dev/null +++ b/reorder-list/ppxyn1.py @@ -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 + +