Skip to content

Commit 7322cc7

Browse files
authored
Merge pull request #2281 from ppxyn1/main
[ppxyn1] WEEK 11 solutions
2 parents 4f00e77 + b632f87 commit 7322cc7

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

merge-intervals/ppxyn1.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# idea : -
2+
# TimeComplexity: # O(n log(n))
3+
class Solution:
4+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
5+
intervals = sorted(intervals)
6+
output = [intervals[0]]
7+
8+
# Merge when intervals overlap
9+
for start, end in intervals[1:]:
10+
lastEnd = output[-1][1]
11+
if start <= lastEnd:
12+
output[-1][1] = max(lastEnd, end)
13+
else:
14+
output.append([start, end])
15+
return output
16+
17+

missing-number/ppxyn1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# idea : -
2+
# Time Complexity : below O(n^2)
3+
4+
class Solution:
5+
def missingNumber(self, nums: List[int]) -> int:
6+
nums.sort()
7+
for i in range(len(nums)):
8+
if nums[i] != i:
9+
return i
10+
return len(nums)
11+
12+

reorder-list/ppxyn1.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# idea: Two-Pointer
2+
# Time Complexixty: O(n)?
3+
'''
4+
Linked lists do not provide a len func, split at the middle and merge the two halves sequentially
5+
'''
6+
7+
# Definition for singly-linked list.
8+
# class ListNode:
9+
# def __init__(self, val=0, next=None):
10+
# self.val = val
11+
# self.next = next
12+
class Solution:
13+
def reorderList(self, head):
14+
if not head or not head.next:
15+
return
16+
17+
# 1. middle
18+
slow, fast = head, head
19+
while fast and fast.next:
20+
slow = slow.next
21+
fast = fast.next.next
22+
23+
# 2. reverse second half
24+
prev = None
25+
cur = slow.next
26+
slow.next = None
27+
28+
while cur:
29+
nxt = cur.next
30+
cur.next = prev
31+
prev = cur
32+
cur = nxt
33+
34+
# 3. merge
35+
first, second = head, prev
36+
while second:
37+
t1, t2 = first.next, second.next
38+
first.next = second
39+
second.next = t1
40+
first = t1
41+
second = t2
42+
43+

0 commit comments

Comments
 (0)