【leetcode】1109. Corporate Flight Bookings
题目如下:
There are
nflights, and they are labeled from1ton.We have a list of flight bookings. The
i-th bookingbookings[i] = [i, j, k]means that we bookedkseats from flights labeleditojinclusive.Return an array
answerof lengthn, representing the number of seats booked on each flight in order of their label.Example 1:
Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output: [10,55,45,25,25]Constraints:
1 <= bookings.length <= 200001 <= bookings[i][0] <= bookings[i][1] <= n <= 200001 <= bookings[i][2] <= 10000
解题思路:这种区间问题,我首先想到的是线段树,当然本题线段树似乎不是最优答案,因为我的解法耗时在2S左右。遍历bookings,把每个item的bookings[i][2]累加到相应的数的节点中,最后统一计算总数即可。
代码如下:
class Solution(object):
def corpFlightBookings(self, bookings, n):
"""
:type bookings: List[List[int]]
:type n: int
:rtype: List[int]
"""
segment = [0] * (4 * n + 1)
def recursive(start,end,low,high,inx,val):
#print start,end,low,high,inx,val
if low > high:
return
if start == low and end == high:
segment[inx] += val
return
mid = (low + high)/2
#if start == low and end == high:
# recursive(start, mid, low, mid, inx * 2, val)
# recursive(mid + 1, end, mid + 1, high, inx * 2 + 1, val)
if end <= mid:
recursive(start,end,low,mid,inx*2,val)
elif start > mid:
recursive(start, end, mid + 1, high, inx * 2+1, val)
else:
recursive(start, mid, low, mid, inx * 2, val)
recursive(mid+1, end, mid+1, high, inx * 2 + 1, val) res = [0] * n
def query(inx,low,high,segment_inx,node_inx):
if segment_inx >= len(segment):
return
mid = (low + high)/2
res[node_inx] += segment[segment_inx]
if inx <= mid:
query(inx,low,mid,segment_inx*2,node_inx)
else:
query(inx, mid+1, high, segment_inx * 2 + 1, node_inx) for (start,end,val) in bookings:
recursive(start,end,1,n,1,val) for i in range(1,n+1):
query(i,1,n,1,i-1)
return res
【leetcode】1109. Corporate Flight Bookings的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- 阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门
H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...
- robotframework(rf)中对时间操作的datetime库常用关键字
1.对固定日期进行操作,增加或减去单位时间或者时间段 2.对两个时间段进行操作 3.对时间格式转化,获取时间戳. 4.从完整时间中取指定年月日等 5.对时间类型进行格式化 6.获取当前时间或者指定时区 ...
- Apache监控调优
apache是一款对静态资源处理得比较好的中间件,但是对动态请求处理得不是很好,tomcat则正好相反. apache运用得比较多得工作模式主要是Prefork和Worker两种模式 1.Prefor ...
- JS函数的基础部分
JS函数的基础部分 JS函数的部分: 先看下一段的代码: window.onload = function(){ function test(){ alert("123"); ...
- Spring获取request、session以及servletContext
获取request: HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestA ...
- 彻底理解RSA算法原理
1. 什么是RSA RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法.在了解RSA算法之前,先熟悉下几个术语 根据密钥的使用方法,可以将密码分为对称密码和公钥密码 对称密码:加 ...
- MySQL学习-数据库设计以及sql的进阶语句
1.数据库设计 关系型数据库建议在E-R模型的基础上,我们需要根据产品经理的设计策划,抽取出来模型与关系,制定出表结构,这是项目开始的第一步 在开发中有很多设计数据库的软件,常用的如power des ...
- python+selenium控制浏览器窗口(刷新、前进、后退、退出浏览器)
调用说明: driver.属性值 变量说明: 1.driver.current_url:用于获得当前页面的URL 2.driver.title:用于获取当前页面的标题 3.driver.page_so ...
- JAVA获取当前系统时间System.currentTimeMillis()以及获取运行时间
System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...
- 通过编写串口助手工具学习MFC过程——(七)添加Tab Control控件
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...