Skip to content

Commit 3b9cbaf

Browse files
update 646
1 parent 765af62 commit 3b9cbaf

File tree

3 files changed

+24
-8
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+24
-8
lines changed

Diff for: paginated_contents/algorithms/1st_thousand/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
| 649 | [Dota2 Senate](https://leetcode.com/problems/dota2-senate/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_649.java) | | Medium | Greedy
226226
| 648 | [Replace Words](https://leetcode.com/problems/replace-words/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_648.java) | | Medium | Trie
227227
| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_647.java) | | Medium | DP
228-
| 646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_646.java) | | Medium | DP, Greedy
228+
| 646 | [Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_646.java) | | Medium | DP, Greedy, Array, Sorting
229229
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_645.java) | | Easy |
230230
| 644 | [Maximum Average Subarray II](https://leetcode.com/problems/maximum-average-subarray-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_644.java) | | Hard | Binary Search
231231
| 643 | [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_643.java) | | Easy |

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_646.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Arrays;
44

55
public class _646 {
6-
76
public static class Solution1 {
87
/**
98
* credit: https://discuss.leetcode.com/topic/96804/java-o-nlog-n-time-o-1-space
@@ -31,16 +30,21 @@ public static class Solution2 {
3130
* credit: https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105623/Java-Very-Simple-without-DP
3231
*/
3332
public int findLongestChain(int[][] pairs) {
33+
//sort by pair[0]
3434
Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
3535
int len = 0;
3636
int pre = Integer.MIN_VALUE;
3737
for (int[] pair : pairs) {
3838
if (pair[0] > pre) {
39-
//not overlap
39+
//no overlap
4040
len++;
41+
//so we need to update the previous number to be the end of current pair: pair[1]
4142
pre = pair[1];
4243
} else if (pair[1] < pre) {
4344
//overlap but with a smaller second number
45+
//since we want to find the maximum possible chain, so we update pre to be this smaller number
46+
//this means we decided to adopt this pair to be in this chain and give up the previous one
47+
//this logic can be seen clearly in test3 for this class
4448
pre = pair[1];
4549
}
4650
}

Diff for: src/test/java/com/fishercoder/firstthousand/_646Test.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.fishercoder.firstthousand;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions.firstthousand._646;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
67

7-
import static org.junit.Assert.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
89

910
public class _646Test {
1011

1112
private static _646.Solution1 solution1;
13+
private static _646.Solution2 solution2;
1214
private static int[][] pairs;
1315

14-
@BeforeClass
15-
public static void setup() {
16+
@BeforeEach
17+
public void setup() {
1618
solution1 = new _646.Solution1();
19+
solution2 = new _646.Solution2();
1720
}
1821

1922
@Test
@@ -25,6 +28,7 @@ public void test1() {
2528
{3, 4}
2629
};
2730
assertEquals(3, solution1.findLongestChain(pairs));
31+
assertEquals(3, solution2.findLongestChain(pairs));
2832
}
2933

3034
@Test
@@ -40,6 +44,14 @@ public void test2() {
4044
{2, 10}
4145
};
4246
assertEquals(2, solution1.findLongestChain(pairs));
47+
assertEquals(2, solution2.findLongestChain(pairs));
48+
}
49+
50+
@Test
51+
public void test3() {
52+
pairs = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[-6,9],[1,6],[8,10],[-1,4],[-6,-2],[-9,8],[-5,3],[0,3]");
53+
assertEquals(3, solution1.findLongestChain(pairs));
54+
assertEquals(3, solution2.findLongestChain(pairs));
4355
}
4456

4557
}

0 commit comments

Comments
 (0)