Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ca9bf66

Browse files
committed
Add solution #646
1 parent ee4187a commit ca9bf66

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

0646-maximum-length-of-pair-chain.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 646. Maximum Length of Pair Chain
3+
* https://leetcode.com/problems/maximum-length-of-pair-chain/
4+
* Difficulty: 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
9+
* this fashion.
10+
*
11+
* Return the length longest chain which can be formed.
12+
*
13+
* You do not need to use up all the given intervals. You can select pairs in any order.
14+
*/
15+
16+
/**
17+
* @param {number[][]} pairs
18+
* @return {number}
19+
*/
20+
var findLongestChain = function(pairs) {
21+
pairs.sort((a, b) => a[1] - b[1]);
22+
let pointer = -Infinity;
23+
let result = 0;
24+
25+
for (const [start, end] of pairs) {
26+
if (start > pointer) {
27+
pointer = end;
28+
result++;
29+
}
30+
}
31+
32+
return result;
33+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@
485485
641|[Design Circular Deque](./0641-design-circular-deque.js)|Medium|
486486
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
487487
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|
488+
646|[Maximum Length of Pair Chain](./0646-maximum-length-of-pair-chain.js)|Medium|
488489
648|[Replace Words](./0648-replace-words.js)|Medium|
489490
649|[Dota2 Senate](./0649-dota2-senate.js)|Medium|
490491
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|

0 commit comments

Comments
 (0)