当前位置:网站首页>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);
}
}边栏推荐
- MATLAB随机函数汇总
- 1.7、PX4遥控器校准
- 云呐|PDA无线固定资产盘点管理系统
- 2021-3-1MATLAB写cnn的mnist数据库训练
- 条码固定资产管理系统的作用,固定资产条码化管理
- Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
- I was so excited about the college entrance examination in 2022
- Threejs: streamer effect encapsulation
- Sealem finance builds Web3 decentralized financial platform infrastructure
- Lazy singleton mode
猜你喜欢

中间件_Redis_06_Redis的事务

SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)
![[ROS introduction] cmakelist Txt and packages XML interpretation](/img/26/bae82a457fb83b2214d2f8c20955e2.jpg)
[ROS introduction] cmakelist Txt and packages XML interpretation

Understanding of multithreading

Leetcode linked list queue stack problem

Detailed explanation of classic papers on OCR character recognition

Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system

1.3 introduction to ROS UAV

Project_ Visual analysis of epidemic data based on Web Crawler

SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
随机推荐
ROS参数服务器
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
IRS application release 15: application security self test guide
Multi interest recall model practice | acquisition technology
Understanding of multithreading
Middleware_ Redis_ 05_ Persistence of redis
“看似抢票实际抢钱”,别被花式抢票产品一再忽悠
After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?
Linux安装mysql数据库详解
Set up a flag -- Reconstruct promise
A/B机器正常连接后, B机器突然重启, 问A此时处于TCP的 什么状态?如何消除服务器程序中的这个状态?
Px4 installation tutorial (VI) vertical fixed wing (tilting)
2021-2-26编程语言知识点整理
Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
项目_基于网络爬虫的疫情数据可视化分析
Leetcode 1574 shortest subarray to be removed to make array sorted
Project_ Visual analysis of epidemic data based on Web Crawler
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)