Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit804505b

Browse files
add 2054
1 parent96ca744 commit804505b

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_2054 {
6+
publicstaticclassSolution1 {
7+
publicintmaxTwoEvents(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 =newint[events.length];
11+
for (inti =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+
intans =0;
19+
for (inti =0;i <events.length;i++) {
20+
intend =events[i][1];
21+
intleft =i +1;
22+
intright =events.length;
23+
while (left <right) {
24+
intmid =left + (right -left) /2;
25+
if (events[mid][0] <=end) {
26+
left =mid +1;
27+
}else {
28+
right =mid;
29+
}
30+
}
31+
intvalue =events[i][2];
32+
if (right >=0 &&right <events.length) {
33+
value +=max[right];
34+
}
35+
ans =Math.max(ans,value);
36+
}
37+
returnans;
38+
}
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1;
5+
importcom.fishercoder.solutions._2054;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importstaticorg.junit.Assert.assertArrayEquals;
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_2054Test {
13+
privatestatic_2054.Solution1solution1;
14+
privatestaticint[][]events;
15+
privatestaticintexpected;
16+
17+
@BeforeClass
18+
publicstaticvoidsetup() {
19+
solution1 =new_2054.Solution1();
20+
}
21+
22+
@Test
23+
publicvoidtest1() {
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+
publicvoidtest2() {
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+
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp