Skip to content

Commit b632f87

Browse files
committed
[:solved] #247, #278
1 parent 2c7db9e commit b632f87

2 files changed

Lines changed: 60 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+

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)