当前位置: 首页 > news >正文

LeetCode1109. Corporate Flight Bookings

文章目录

    • 一、题目
    • 二、题解

一、题目

There are n flights that are labeled from 1 to n.

You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi seats reserved for each flight in the range.

Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i.

Example 1:

Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output: [10,55,45,25,25]
Explanation:
Flight labels: 1 2 3 4 5
Booking 1 reserved: 10 10
Booking 2 reserved: 20 20
Booking 3 reserved: 25 25 25 25
Total seats: 10 55 45 25 25
Hence, answer = [10,55,45,25,25]
Example 2:

Input: bookings = [[1,2,10],[2,2,15]], n = 2
Output: [10,25]
Explanation:
Flight labels: 1 2
Booking 1 reserved: 10 10
Booking 2 reserved: 15
Total seats: 10 25
Hence, answer = [10,25]

Constraints:

1 <= n <= 2 * 104
1 <= bookings.length <= 2 * 104
bookings[i].length == 3
1 <= firsti <= lasti <= n
1 <= seatsi <= 104

二、题解

class Solution {
public:vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {vector<int> cnt(n+2,0);for(auto x:bookings){cnt[x[0]] += x[2];cnt[x[1] + 1] -= x[2];}for(int i = 1;i <= n;i++){cnt[i] += cnt[i-1];}vector<int> res(n,0);for(int i = 0;i < n;i++){res[i] = cnt[i+1];}return res;}
};
http://www.lryc.cn/news/280173.html

相关文章:

  • 视觉SLAM十四讲|【五】相机与IMU时间戳同步
  • js null和undefined的区别
  • Arduino| IDE下载、安装和设置以及开发板的连接
  • Linux之Ubuntu环境Jenkins部署前端项目
  • QT下的几种实现modbus的库,记录
  • HarmonyOS4.0系统性深入开发18公共事件简介
  • 华为路由器OSPF动态链路路由协议配置
  • 常用注解/代码解释(仅个人使用)
  • 2024阿里云服务器ECS介绍_全方位解析_CPU性能详解
  • 向伟人学习反焦虑,在逆境中崛起
  • 线上问题整理
  • 【elastic search】详解elastic search集群
  • 近红外光谱分析技术与基于深度学习的化学计量学方法
  • Elasticsearch windows开箱即用【记录】
  • 第 3 课 ROS 常用术语及命令说明
  • 基于AidLux的智慧教育版面分析应用
  • Spring | Spring框架最基本核心的jar包、Spring的入门程序、依赖注入
  • [晓理紫]每日论文推送(有中文摘要,源码或项目地址)--大模型相关、扩散模型、视觉导航
  • 【软件项目管理_软件工程】软件项目管理课后相关习题
  • Docker-Compose:教你入门到精通
  • 软件设计不是CRUD(10):低耦合模块设计理论——业务抽象:从需求中提取业务维度
  • S1-08 流和消息缓冲区
  • Java重修第五天—面向对象3
  • 【征稿进行时|见刊、检索快速稳定】2024年经济发展与旅游管理国际学术会议(ICEDTM 2024)
  • 瑞_Java开发手册_(四)安全规约
  • Docker 安全必知:最佳实践、漏洞管理与监控策略
  • 【Flutter】多线程
  • STM32-实时时钟RTC-1
  • node(express.js创建项目)+连接mysql数据库
  • 【FLV】记录 H.264的解析