-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_linked_list_ranges.py
76 lines (61 loc) · 1.86 KB
/
2_linked_list_ranges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## APPROACH 2
class Range:
low = 0
high = 0
next_range = None
prev_range = None
def __init__(self, n):
self.low=self.high=n
def __str__(self):
return "(%s, %s)" % (self.low, self.high)
def solution(A):
r = Range(0)
for n in A:
curr = r
while True:
print("(%s, %s)" % (curr.low, curr.high))
if not curr.next_range:
break
curr = curr.next_range
print(" >>> %s " % n)
if n < 1:
continue
action = False
curr = r
while True:
if curr.low <= n and curr.high >= n:
print("absorbed")
action = True
if curr.high == n-1:
print("enlarging")
action = True
curr.high = n
if curr.next_range and curr.next_range.low == n + 1:
print("connecting")
curr.high = curr.next_range.high
curr.next_range = curr.next_range.next_range
if curr.low > n:
r2 = Range(n)
r2.next_range = curr
curr.prev_range.next_range = r2
curr.prev_range = r2
print("inserting")
action = True
if action:
break
if not curr.next_range:
break
curr=curr.next_range
if not action:
r3 = Range(n)
r3.prev_range = curr
r3.next_range = curr.next_range
curr.next_range = r3
print("appending")
curr = r
while True:
print("(%s, %s)" % (curr.low, curr.high))
if not curr.next_range:
break
curr = curr.next_range
return r.high + 1