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

Commit 804505b

Browse files
add 2054
1 parent 96ca744 commit 804505b

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|2055|[Plates Between Candles](https://leetcode.com/problems/plates-between-candles/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2055.java) ||Medium||
12+
|2054|[Two Best Non-Overlapping Events](https://leetcode.com/problems/two-best-non-overlapping-events/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2054.java) ||Medium||
1213
|2053|[Kth Distinct String in an Array](https://leetcode.com/problems/kth-distinct-string-in-an-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2053.java) ||Easy||
1314
|2050|[Parallel Courses III](https://leetcode.com/problems/parallel-courses-iii/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2050.java) ||Hard||
1415
|2048|[Next Greater Numerically Balanced Number](https://leetcode.com/problems/next-greater-numerically-balanced-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2048.java) ||Medium||
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _2054 {
6+
public static class Solution1 {
7+
public int maxTwoEvents(int[][] events) {
8+
/**Credit: https://leetcode.com/nevergiveup/ on https://leetcode.com/contest/biweekly-contest-64/ranking/*/
9+
Arrays.sort(events, (a, b) -> a[0] - b[0]);
10+
int[] max = new int[events.length];
11+
for (int i = events.length - 1; i >= 0; i--) {
12+
if (i == events.length - 1) {
13+
max[i] = events[i][2];
14+
} else {
15+
max[i] = Math.max(events[i][2], max[i + 1]);
16+
}
17+
}
18+
int ans = 0;
19+
for (int i = 0; i < events.length; i++) {
20+
int end = events[i][1];
21+
int left = i + 1;
22+
int right = events.length;
23+
while (left < right) {
24+
int mid = left + (right - left) / 2;
25+
if (events[mid][0] <= end) {
26+
left = mid + 1;
27+
} else {
28+
right = mid;
29+
}
30+
}
31+
int value = events[i][2];
32+
if (right >= 0 && right < events.length) {
33+
value += max[right];
34+
}
35+
ans = Math.max(ans, value);
36+
}
37+
return ans;
38+
}
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1;
5+
import com.fishercoder.solutions._2054;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertArrayEquals;
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _2054Test {
13+
private static _2054.Solution1 solution1;
14+
private static int[][] events;
15+
private static int expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _2054.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
events = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3,2],[4,5,2],[2,4,3]");
25+
expected = 4;
26+
assertEquals(expected, solution1.maxTwoEvents(events));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
events = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3,2],[4,5,2],[1,5,5]");
32+
expected = 5;
33+
assertEquals(expected, solution1.maxTwoEvents(events));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
events = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,5,3],[1,5,1],[6,6,5]");
39+
expected = 8;
40+
assertEquals(expected, solution1.maxTwoEvents(events));
41+
}
42+
43+
44+
}

0 commit comments

Comments
 (0)