leetcode2054

leetcode 2054

cpp 复制代码
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>


using namespace std;

struct Event {
    // 时间戳
    int ts;
    // op = 0 表示左边界,op = 1 表示右边界
    int op;
    int val;
    Event(int _ts, int _op, int _val): ts(_ts), op(_op), val(_val) {}
    bool operator< (const Event& that) const {
        return tie(ts, op) < tie(that.ts, that.op);
    }
};

class Solution {
public:
    int maxTwoEvents(vector<vector<int> >& events) {
        vector<Event> evs;
        for (const auto& event : events) {
            evs.emplace_back(event[0], 0, event[2]);
            evs.emplace_back(event[1], 1, event[2]);
        }
        sort(evs.begin(), evs.end());

        for (const auto& row : evs) {
            
            std::cout << row.ts << ","<< row.op << ","<< row.val << " ";
            
            std::cout << std::endl;
        }
        
        
        int ans = 0, bestFirst = 0;
        for(int i=0; i<evs.size(); i++){
            int cur_ts = evs[i].ts;
            int cur_op = evs[i].op;
            int cur_val = evs[i].val;

            if (cur_op == 0) {
                ans = max(ans, cur_val + bestFirst);
            }
            else {
                bestFirst = max(bestFirst, cur_val);
            };

        }

        return ans;
    }
};

int main(){
    vector<vector<int> > events = {{1,3,2},{4,5,2},{2,4,3}};

    // for (const auto& row : events) {
    //     for (const auto& element : row) {
    //         std::cout << element << " ";
    //     }
    //     std::cout << std::endl;
    // }
    Solution s;
    int ans = s.maxTwoEvents(events);
    
    return 0;
}

(ith start_time, ith end_time, ith val) 需要找到前i-1 end_time 的最大值,加上ith val, 遍历所有时间,找出最大值。

相关推荐
过客随尘11 分钟前
byte如何正确转int
后端·算法
seanmeng202239 分钟前
理解AWS AgentCore - Long term Memory
算法
ygming1 小时前
Q55- code34- 在排序数组中查找元素的第一个和最后一个位置 + Q56- code33- 搜索旋转排序数组
前端·算法
qiuyunoqy3 小时前
蓝桥杯算法之搜索章 - 4
算法
Swaggy T3 小时前
自动驾驶决策算法 —— 有限状态机 FSM
linux·人工智能·算法·机器学习·自动驾驶
MATLAB代码顾问3 小时前
MATLAB实现遗传算法求解路网路由问题
开发语言·算法·matlab
啊阿狸不会拉杆3 小时前
《算法导论》第 21 章-用于不相交集合的数据结构
数据结构·c++·算法·随机森林
快去睡觉~3 小时前
力扣11:盛水最多的容器
算法·leetcode·职场和发展
楽码4 小时前
了解HMAC及实现步骤
后端·算法·微服务
CoovallyAIHub4 小时前
YOLOVision 2025 官宣日期!大会议程暗藏 YOLOv14 发布信号?
深度学习·算法·计算机视觉