-
Notifications
You must be signed in to change notification settings - Fork 44
/
maximum-length-of-pair-chain.py
210 lines (183 loc) · 6.05 KB
/
maximum-length-of-pair-chain.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
646. Maximum Length of Pair Chain
Medium
You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
Return the length longest chain which can be formed.
You do not need to use up all the given intervals. You can select pairs in any order.
Example 1:
Input: pairs = [[1,2],[2,3],[3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4].
Example 2:
Input: pairs = [[1,2],[7,8],[4,5]]
Output: 3
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
Constraints:
n == pairs.length
1 <= n <= 1000
-1000 <= lefti < righti <= 1000
"""
# V0
# IDEA : GREEDY + sorting + 2 pointers
# IDEA :
# -> SORT ON "1st element" (0 index)
# -> define i pointer, cnt
# -> loop over pairts
# -> if j == 0 or pairs[j][0] > pairs[i][1]
# -> make i = j, and cnt += 1
class Solution(object):
def findLongestChain(self, pairs):
pairs.sort(key=lambda x: x[1])
cnt = 0
i = 0
for j in range(len(pairs)):
if j == 0 or pairs[j][0] > pairs[i][1]:
i = j
cnt += 1
return cnt
# V0
# IDEA : GREEDY + sorting
# -> we sort on pair's "2nd" element (0 index) -> possible cases that we can get sub pairs with max length with the needed conditions
# -> we need to find the "max length" of "continous or non-continous" sub pairs (with condition)
# -> so start from the "sorted 1st pair" CAN ALWAYS MAKE US GET THE MAX LENGTH of sub pairs with the condition ( we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.)
##########
# DEMO
# ...:
# ...: x = [[-10,-8],[8,9],[-5,0],[6,10],[-6,-4],[1,7],[9,10],[-4,7]]
# ...: s = Solution()
# ...: r = s.findLongestChain(x)
# ...: print (r)
# pairs = [[-10, -8], [-6, -4], [-5, 0], [1, 7], [-4, 7], [8, 9], [6, 10], [9, 10]]
# x = -10 y = -8
# currTime = -8 ans = 1
# x = -6 y = -4
# currTime = -4 ans = 2
# x = -5 y = 0
# currTime = -4 ans = 2
# x = 1 y = 7
# currTime = 7 ans = 3
# x = -4 y = 7
# currTime = 7 ans = 3
# x = 8 y = 9
# currTime = 9 ans = 4
# x = 6 y = 10
# currTime = 9 ans = 4
# x = 9 y = 10
# currTime = 9 ans = 4
# 4
class Solution(object):
def findLongestChain(self, pairs):
pairs = sorted(pairs, key=lambda x : x[1])
### NOTICE HERE
currTime, ans = float('-inf'), 0
for x, y in pairs:
### NOTICE HERE
if currTime < x:
currTime = y
ans += 1
return ans
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79826524
# http://bookshadow.com/weblog/2017/07/24/leetcode-maximum-length-of-pair-chain/
# IDEA : GREEDY
class Solution(object):
def findLongestChain(self, pairs):
"""
:type pairs: List[List[int]]
:rtype: int
"""
pairs.sort(key=lambda x: x[1])
currTime, ans = float('-inf'), 0
for x, y in pairs:
if currTime < x:
currTime = y
ans += 1
return ans
### Test case :
s=Solution()
assert s.findLongestChain([[1,2], [2,3], [3,4]]) == 2
assert s.findLongestChain([]) == 0
assert s.findLongestChain([[1,1]]) == 1
assert s.findLongestChain([[1,2]]) == 1
assert s.findLongestChain([[1,2], [2,3], [3,4], [9,10]]) == 3
assert s.findLongestChain([[1,2], [2,3], [2,3], [3,4]]) == 2
assert s.findLongestChain([[1,2], [1,2], [1,2]]) == 1
assert s.findLongestChain([[i, i+1] for i in range(10)]) == 5
# V1'
# https://leetcode.com/problems/maximum-length-of-pair-chain/solution/
# IDEA : DP
# time complexity : O(N^2)
# space complexity : O(N)
class Solution(object): #Time Limit Exceeded
def findLongestChain(self, pairs):
pairs.sort()
dp = [1] * len(pairs)
for j in range(len(pairs)):
for i in range(j):
if pairs[i][1] < pairs[j][0]:
dp[j] = max(dp[j], dp[i] + 1)
return max(dp)
# V1''
# https://leetcode.com/problems/maximum-length-of-pair-chain/solution/
# IDEA : GREEDY
class Solution(object):
def findLongestChain(self, pairs):
cur, ans = float('-inf'), 0
for x, y in sorted(pairs, key = operator.itemgetter(1)):
if cur < x:
cur = y
ans += 1
return ans
# V1'''
# https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105608/Python-DP-solution
# IDEA : DP
class Solution(object):
def findLongestChain(self, pairs):
"""
:type pairs: List[List[int]]
:rtype: int
"""
pairs.sort()
n = len(pairs)
dp = [1] * n
for i in range(n):
for j in range(i):
if pairs[i][0] > pairs[j][1] and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
return max(dp)
# V1''''
# https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105640/O(nlogn)-Python-solution-binary-search-easy-to-understand
# IDEA : BINARY SEARCH
class Solution(object):
def findLongestChain(self, pairs):
"""
:type pairs: List[List[int]]
:rtype: int
"""
# sort by x for pairs (x1, y1), (x2, y2), (x3, y3)...
pairs.sort()
# min_end_y[i] is the ending tuple minimum y of length=i chain
min_end_y = [float('inf')] * len(pairs)
for x, y in pairs:
# since (a, b) can chain (c, d) iff b < c, use bisect_left
i = bisect.bisect_left(min_end_y, x)
# greedy method, for the same length chain, the smaller y the better
min_end_y[i] = min(min_end_y[i], y)
return bisect.bisect_left(min_end_y, float('inf'))
# V2
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def findLongestChain(self, pairs):
"""
:type pairs: List[List[int]]
:rtype: int
"""
pairs.sort(key=lambda x: x[1])
cnt, i = 0, 0
for j in range(len(pairs)):
if j == 0 or pairs[i][1] < pairs[j][0]:
cnt += 1
i = j
return cnt