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

Commit f21b148

Browse files
refactor 646
1 parent 811b58d commit f21b148

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,26 @@ Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c
2121
*/
2222
public class _646 {
2323

24-
/**
25-
* credit: https://discuss.leetcode.com/topic/96804/java-o-nlog-n-time-o-1-space
26-
*/
27-
public int findLongestChain(int[][] pairs) {
28-
Arrays.sort(pairs, (o1, o2) -> o1[1] - o2[1]);
29-
int result = 0;
30-
int n = pairs.length;
31-
int i = -1;
32-
while (++i < n) {
33-
result++;
34-
int curEnd = pairs[i][1];
35-
while (i + 1 < n && pairs[i + 1][0] <= curEnd) {
36-
/**This means, we'll keep incrementing i until pairs[i+1][0] is
37-
* exactly greater than curEnd.*/
38-
i++;
24+
public static class Solution1 {
25+
/**
26+
* credit: https://discuss.leetcode.com/topic/96804/java-o-nlog-n-time-o-1-space
27+
*/
28+
public int findLongestChain(int[][] pairs) {
29+
Arrays.sort(pairs, (o1, o2) -> o1[1] - o2[1]);
30+
int result = 0;
31+
int n = pairs.length;
32+
int i = -1;
33+
while (++i < n) {
34+
result++;
35+
int curEnd = pairs[i][1];
36+
while (i + 1 < n && pairs[i + 1][0] <= curEnd) {
37+
/**This means, we'll keep incrementing i until pairs[i+1][0] is
38+
* exactly greater than curEnd.*/
39+
i++;
40+
}
3941
}
42+
return result;
4043
}
41-
return result;
4244
}
4345

4446
}

src/test/java/com/fishercoder/_646Test.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,38 @@
88

99
public class _646Test {
1010

11-
private static _646 test;
11+
private static _646.Solution1 solution1;
1212
private static int[][] pairs;
1313

1414
@BeforeClass
1515
public static void setup() {
16-
test = new _646();
16+
solution1 = new _646.Solution1();
1717
}
1818

1919
@Test
2020
public void test1() {
2121
pairs = new int[][]{
22-
{1,2},
23-
{2,3},
24-
{5,6},
25-
{3,4}
22+
{1, 2},
23+
{2, 3},
24+
{5, 6},
25+
{3, 4}
2626
};
27-
assertEquals(3, test.findLongestChain(pairs));
27+
assertEquals(3, solution1.findLongestChain(pairs));
2828
}
2929

3030
@Test
3131
public void test2() {
3232
pairs = new int[][]{
33-
{9,10},
34-
{-9,9},
35-
{-6,1},
36-
{-4,1},
37-
{8,10},
38-
{7,10},
39-
{9,10},
40-
{2,10}
33+
{9, 10},
34+
{-9, 9},
35+
{-6, 1},
36+
{-4, 1},
37+
{8, 10},
38+
{7, 10},
39+
{9, 10},
40+
{2, 10}
4141
};
42-
assertEquals(2, test.findLongestChain(pairs));
42+
assertEquals(2, solution1.findLongestChain(pairs));
4343
}
4444

4545
}

0 commit comments

Comments
 (0)