python算法 前缀和
2024-09-06 10:30:03
这里有 n 个航班,它们分别从 1 到 n 进行编号。
有一份航班预订表 bookings ,表中第 i 条预订记录 bookings[i] = [firsti, lasti, seatsi] 意味着在从 firsti 到 lasti (包含 firsti 和 lasti )的 每个航班 上预订了 seatsi 个座位。
请你返回一个长度为 n 的数组 answer,里面的元素是每个航班预定的座位总数。
示例 1:
输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
输出:[10,55,45,25,25]
解释:
航班编号 1 2 3 4 5
预订记录 1 : 10 10
预订记录 2 : 20 20
预订记录 3 : 25 25 25 25
总座位数: 10 55 45 25 25
因此,answer = [10,55,45,25,25]
示例 2:
输入:bookings = [[1,2,10],[2,2,15]], n = 2
输出:[10,25]
解释:
航班编号 1 2
预订记录 1 : 10 10
预订记录 2 : 15
总座位数: 10 25
因此,answer = [10,25]
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
#创建一个和n长度一致的列表
base=[0 for i in range(n)]
for first , last , seat in bookings:
#只把所有first的加上,last的值减去
base[first-1] += seat
if last < n:
base[last] -=seat
#然后把前缀相加求和
temp=0
result=[]
for i in range(n):
temp +=base[i]
result.append(temp)
return result
最新文章
- JSONArray的应用
- 搭建centos测试环境:window安装xshell,WinSCP 。 centos安装jdk tomcat
- 推流和拉流的概念以及RTMP和HLS协议
- activeMQ 安装于使用
- Python笔记-built-in函数,文件操作,lambda函数
- 【转】Ant学习笔记——自己构建Ant编译环境
- SQLSERVER 2012之AlwaysOn -- 同步模式下的网卡性能优化
- 【原】对频率论(Frequentist)方法和贝叶斯方法(Bayesian Methods)的一个总结
- 20150914 异常语句 math的方法 去空格 索引
- 在eclipse中运行storm-starter
- ASP.net+MVC--2
- A. Initial Bet(Codeforces Round #273)
- JavaScript(五):函数(闭包,eval)
- Android进阶(十六)子线程调用Toast报Can&#39;t create handler inside thread that has not called Looper.prepare() 错误
- Snapde电子表格支持的文件格式
- 【RL-TCPnet网络教程】第2章 嵌入式网络协议栈基础知识
- ORA-12557协议适配器不可加载
- spring cloud (二、服务注册安全demo_eureka)
- 类 __new__方法实现单例
- (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)