Skip to content

Commit 47cbdb3

Browse files
committed
update 646, cheatsheet
1 parent 664af21 commit 47cbdb3

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@
942942
452 | [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [Python](./leetcode_python/Greedy/minimum-number-of-arrows-to-burst-balloons.py) | _O(nlogn)_ | _O(1)_ | Medium | | AGAIN*
943943
455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies/) | [Python](./leetcode_python/Greedy/assign-cookies.py) | _O(nlogn)_ | _O(1)_ | Easy | | OK*
944944
621 | [Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [Python](./leetcode_python/Greedy/task-scheduler.py) | _O(n)_ | _O(1)_ | Medium |`trick`,`array`,`greedy`, `fb` | AGAIN****** (6)
945-
646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [Python](./leetcode_python/Greedy/maximum-length-of-pair-chain.py) | _O(nlogn)_ | _O(1)_ | Medium | good basic, similar as `#435 Non-overlapping Intervals`, Line Sweep, `amazon` | AGAIN*** (3)
945+
646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [Python](./leetcode_python/Greedy/maximum-length-of-pair-chain.py) | _O(nlogn)_ | _O(1)_ | Medium | good trick, greedy, similar as `#435 Non-overlapping Intervals`, Line Sweep, `amazon` | OK****** (4) (but again)
946946
649 | [Dota2 Senate](https://leetcode.com/problems/dota2-senate/) | [Python](./leetcode_python/Greedy/dota2-senate.py) | _O(n)_ | _O(n)_ | Medium | complex question | AGAIN (not start*)
947947
659 | [Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Python](./leetcode_python/Greedy/split-array-into-consecutive-subsequences.py) | _O(n)_ | _O(1)_ | Medium | | AGAIN (not start*)
948948
738 | [Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/) | [Python](./leetcode_python/Greedy/monotone-increasing-digits.py) | _O(1)_ | _O(1)_ | Medium | good trick, greedy, string, trick, `Amazon`| AGAIN********* (4)

doc/cheatsheet/greedy.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,50 @@ class Solution(object):
152152
# Be aware of it : if there is no valid "v", then the while loop will break automatically at this condition (stop = True)
153153
if stop: break
154154
return ans[1:] if len(ans[1:]) == len(S) else ''
155+
```
156+
157+
### 2-7) Maximum Length of Pair Chain
158+
```python
159+
# LC 646 Maximum Length of Pair Chain
160+
# V0
161+
# IDEA : GREEDY + sorting
162+
# -> we sort on pair's 1st element -> possible cases that we can get sub pairs with max length with the needed conditions
163+
# -> we need to find the "max length" of "continous or non-continous" sub pairs (with condition)
164+
# -> 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.)
165+
##########
166+
# DEMO
167+
# ...:
168+
# ...: x = [[-10,-8],[8,9],[-5,0],[6,10],[-6,-4],[1,7],[9,10],[-4,7]]
169+
# ...: s = Solution()
170+
# ...: r = s.findLongestChain(x)
171+
# ...: print (r)
172+
# pairs = [[-10, -8], [-6, -4], [-5, 0], [1, 7], [-4, 7], [8, 9], [6, 10], [9, 10]]
173+
# x = -10 y = -8
174+
# currTime = -8 ans = 1
175+
# x = -6 y = -4
176+
# currTime = -4 ans = 2
177+
# x = -5 y = 0
178+
# currTime = -4 ans = 2
179+
# x = 1 y = 7
180+
# currTime = 7 ans = 3
181+
# x = -4 y = 7
182+
# currTime = 7 ans = 3
183+
# x = 8 y = 9
184+
# currTime = 9 ans = 4
185+
# x = 6 y = 10
186+
# currTime = 9 ans = 4
187+
# x = 9 y = 10
188+
# currTime = 9 ans = 4
189+
# 4
190+
class Solution(object):
191+
def findLongestChain(self, pairs):
192+
pairs = sorted(pairs, key=lambda x : x[1])
193+
### NOTICE HERE
194+
currTime, ans = float('-inf'), 0
195+
for x, y in pairs:
196+
### NOTICE HERE
197+
if currTime < x:
198+
currTime = y
199+
ans += 1
200+
return ans
155201
```

doc/progress.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
- (normal order)
1010
- (pdf : p.13)
1111
- 776 (again!)
12-
- 646
12+
- 646 (again!)
13+
- 435
1314
- =======
1415
- **back track**
1516
- Binary Search Tree

leetcode_python/Greedy/maximum-length-of-pair-chain.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1+
"""
2+
3+
646. Maximum Length of Pair Chain
4+
Medium
5+
6+
You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
7+
8+
A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
9+
10+
Return the length longest chain which can be formed.
11+
12+
You do not need to use up all the given intervals. You can select pairs in any order.
13+
14+
15+
16+
Example 1:
17+
18+
Input: pairs = [[1,2],[2,3],[3,4]]
19+
Output: 2
20+
Explanation: The longest chain is [1,2] -> [3,4].
21+
Example 2:
22+
23+
Input: pairs = [[1,2],[7,8],[4,5]]
24+
Output: 3
25+
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
26+
27+
28+
Constraints:
29+
30+
n == pairs.length
31+
1 <= n <= 1000
32+
-1000 <= lefti < righti <= 1000
33+
34+
"""
35+
136
# V0
2-
# IDEA : GREEDY
37+
# IDEA : GREEDY + sorting
38+
# -> we sort on pair's 1st element -> possible cases that we can get sub pairs with max length with the needed conditions
39+
# -> we need to find the "max length" of "continous or non-continous" sub pairs (with condition)
40+
# -> 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.)
41+
##########
42+
# DEMO
43+
# ...:
44+
# ...: x = [[-10,-8],[8,9],[-5,0],[6,10],[-6,-4],[1,7],[9,10],[-4,7]]
45+
# ...: s = Solution()
46+
# ...: r = s.findLongestChain(x)
47+
# ...: print (r)
48+
# pairs = [[-10, -8], [-6, -4], [-5, 0], [1, 7], [-4, 7], [8, 9], [6, 10], [9, 10]]
49+
# x = -10 y = -8
50+
# currTime = -8 ans = 1
51+
# x = -6 y = -4
52+
# currTime = -4 ans = 2
53+
# x = -5 y = 0
54+
# currTime = -4 ans = 2
55+
# x = 1 y = 7
56+
# currTime = 7 ans = 3
57+
# x = -4 y = 7
58+
# currTime = 7 ans = 3
59+
# x = 8 y = 9
60+
# currTime = 9 ans = 4
61+
# x = 6 y = 10
62+
# currTime = 9 ans = 4
63+
# x = 9 y = 10
64+
# currTime = 9 ans = 4
65+
# 4
366
class Solution(object):
467
def findLongestChain(self, pairs):
568
pairs = sorted(pairs, key=lambda x : x[1])

0 commit comments

Comments
 (0)