java: add «2054. Two Best Non-Overlapping Events»
URL: https://leetcode.com/problems/two-best-non-overlapping-events/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
b4a300bec2
commit
a1dc25b5e5
1 changed files with 29 additions and 0 deletions
29
java/two-best-non-overlapping-events.java
Normal file
29
java/two-best-non-overlapping-events.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
class Solution {
|
||||||
|
private static final int END = 0;
|
||||||
|
private static final int START = 1;
|
||||||
|
|
||||||
|
private record Event(int time, int type, int payoff) {}
|
||||||
|
|
||||||
|
public int maxTwoEvents(int[][] events) {
|
||||||
|
var times = new ArrayList<Event>();
|
||||||
|
for (var event : events) {
|
||||||
|
times.add(new Event(event[0], START, event[2]));
|
||||||
|
times.add(new Event(event[1] + 1, END, event[2]));
|
||||||
|
}
|
||||||
|
times.sort(Comparator.comparing(Event::time).thenComparing(Event::type));
|
||||||
|
|
||||||
|
int answer = 0, maxPayoff = 0;
|
||||||
|
for (var time : times) {
|
||||||
|
switch (time.type) {
|
||||||
|
case START:
|
||||||
|
answer = Math.max(answer, time.payoff + maxPayoff);
|
||||||
|
break;
|
||||||
|
case END:
|
||||||
|
maxPayoff = Math.max(maxPayoff, time.payoff);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue