当前位置:网站首页>Leetcode 2054 two best non overlapping events
Leetcode 2054 two best non overlapping events
2022-06-11 01:43:00 【_ TCgogogo_】
You are given a 0-indexed 2D integer array of events where events[i] = [startTimei, endTimei, valuei]. The ith event starts at startTimei and ends at endTimei, and if you attend this event, you will receive a value of valuei. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.
Return this maximum sum.
Note that the start time and end time is inclusive: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time t, the next event must start at or after t + 1.
Example 1:

Input: events = [[1,3,2],[4,5,2],[2,4,3]] Output: 4 Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
Example 2:

Input: events = [[1,3,2],[4,5,2],[1,5,5]] Output: 5 Explanation: Choose event 2 for a sum of 5.
Example 3:

Input: events = [[1,5,3],[1,5,1],[6,6,5]] Output: 8 Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8
Constraints:
2 <= events.length <= 105events[i].length == 31 <= startTimei <= endTimei <= 1091 <= valuei <= 106
Topic link :https://leetcode.com/problems/two-best-non-overlapping-events/
The main idea of the topic : At most two disjoint intervals can be selected , Ask for it value The maximum value of and
Topic analysis : Sort left endpoint , Preprocess each section to the rightmost maximum value value , Enumerate left endpoints , Split the right end point
40ms, Time beats 81.8%
class Solution {
class Event implements Comparable {
int st, ed, val;
Event(int st, int ed, int val) {
this.st = st;
this.ed = ed;
this.val = val;
}
@Override
public int compareTo(Object o) {
Event e = (Event) o;
if (this.st > e.st) {
return 1;
} else if (this.st < e.st) {
return -1;
}
return 0;
}
}
// find pos of first event which st value larger than x
int bsearch(Event[] e, int l, int r, int x) {
int mid = 0, ans = -1;
while (l <= r) {
mid = (l + r) >> 1;
if (e[mid].st > x) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
public int maxTwoEvents(int[][] events) {
int n = events.length;
Event[] e = new Event[n];
for (int i = 0; i < n; i++) {
e[i] = new Event(events[i][0], events[i][1], events[i][2]);
}
Arrays.sort(e);
int cur = 0;
int[] ma = new int[n];
for (int i = n - 1; i >= 0; i--) {
if (cur < e[i].val) {
cur = e[i].val;
}
ma[i] = cur;
}
int ans = 0;
for (int i = 0; i < n; i++) {
int pos = bsearch(e, i, n - 1, e[i].ed);
if (pos != -1) {
ans = Math.max(ans, e[i].val + ma[pos]);
}
}
return Math.max(ma[0], ans);
}
}边栏推荐
- Middleware_ Redis_ 05_ Persistence of redis
- hooks的设计哲学
- 2022.6.6-----leetcode. seven hundred and thirty-two
- Project_ Visual analysis of epidemic data based on Web Crawler
- 函数的节流和防抖
- 2.0、ROS与PX4通信详解
- ROS parameter server
- 1.3 ROS 无人机简介
- [Li mu] how to read papers [intensive reading of papers]
- Digital IC Design self-study ing
猜你喜欢

Project_ Visual analysis of epidemic data based on Web Crawler

2.2. Ros+px4 simulation multi-point cruise flight - Square

焱融看|混合云环境下,如何实现数据湖最优存储解决方案
![[VBA Script] extract the information and pending status of all annotations in the word document](/img/dc/0db51d092cde019cef4113796e4882.png)
[VBA Script] extract the information and pending status of all annotations in the word document

Leetcode linked list queue stack problem

2.0 detailed explanation of ROS and Px4 communication

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture
![[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design](/img/63/3193186820215b9babc3d00e1ef20b.jpg)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design

中间件_Redis_06_Redis的事务

ROS parameter server
随机推荐
[interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
Leetcode 1814 count nice pairs in an array (recommended by map)
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
Brief description of custom annotations
Leetcode linked list queue stack problem
[mavros] mavros startup Guide
How to download web photos
复利的保险理财产品怎么样?可以买吗?
Introduction to the policy support of Shenzhen China Patent Award, with a subsidy of 1million yuan
2021-07-18 ROS笔记-基础和通讯
中间件_Redis_06_Redis的事务
Leetcode binary tree problem
2.2、ROS+PX4仿真多点巡航飞行----正方形
Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
如何下载网页照片
SSH Remote Login configuration sshd_ Config file details
Hooks' design philosophy
2.1、ROS+PX4仿真---定点飞行控制
多兴趣召回模型实践|得物技术