Skip to content

Commit a33a2bf

Browse files
refactor 646
1 parent 274a01e commit a33a2bf

File tree

1 file changed

+22
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+22
-0
lines changed

src/main/java/com/fishercoder/solutions/_646.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,26 @@ public int findLongestChain(int[][] pairs) {
2626
}
2727
}
2828

29+
public static class Solution2 {
30+
/**
31+
* credit: https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105623/Java-Very-Simple-without-DP
32+
*/
33+
public int findLongestChain(int[][] pairs) {
34+
Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
35+
int len = 0;
36+
int pre = Integer.MIN_VALUE;
37+
for (int[] pair : pairs) {
38+
if (pair[0] > pre) {
39+
//not overlap
40+
len++;
41+
pre = pair[1];
42+
} else if (pair[1] < pre) {
43+
//overlap but with a smaller second number
44+
pre = pair[1];
45+
}
46+
}
47+
return len;
48+
}
49+
}
50+
2951
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy