【leetcode】1109. Corporate Flight Bookings
题目如下:
There are
n
flights, and they are labeled from1
ton
.We have a list of flight bookings. The
i
-th bookingbookings[i] = [i, j, k]
means that we bookedk
seats from flights labeledi
toj
inclusive.Return an array
answer
of 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 <= 20000
1 <= bookings[i][0] <= bookings[i][1] <= n <= 20000
1 <= 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 ...
随机推荐
- Android下Native的so编译:使用ndk-build.cmd/.sh
最近将一个DLL库移植至安卓下,编译出so文件. 经历makefile.cmake等等的入门到放弃..... 最后还是使用android的ndk编译命令来解决 每个NDK文件下,均包含的是所有工具链. ...
- oracle 普通数据文件备份与恢复
普通数据文件指:非system表空间.undo_tablespace表空间.临时表空间和只读表空间的数据文件.它们损坏导致用户数据不能访问,不会导致db自身异常.实例崩溃.数据库不恢复就无法启动的情况 ...
- 获取使用GitHub api和Jira api Authentication的方法
近段时间在搭建我司的用例管理平台,有如下需求: 1.需要根据项目--版本--轮次的形式来管理项目用例,用例统一保存在git工程. 2.执行用例时,如果用例执行失败,可以通过平台在Jira上提bug. ...
- Django-DRF组件学习-视图学习
1.请求与响应 drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. Djan ...
- 【HBase】三、HBase和RDBMS的比较
HBase作为一种NoSQL的数据库,和传统的类似于mysql这样的关系型数据库是有很大区别的,本文来对他们做一个对比分析,以便更加深入的了解HBase. 主要区别体现在以下六个方面: 1 ...
- 杭州集训Day4
别问我为什么没有前三天,有时间再补~ 60+60+50=170. T1 . 坐等 memset0 ( 1s 256MB )( 原题:洛谷CF1151E Number of Components ) 树 ...
- GraphQL入门有这一篇就足够了
GraphQL入门有这一篇就足够了:https://blog.csdn.net/qq_41882147/article/details/82966783 版权声明:本文为博主原创文章,遵循 CC 4. ...
- SGU 521 North-East ( 二维LIS 线段树优化 )
521. "North-East" Time limit per test: 0.5 second(s)Memory limit: 262144 kilobytes input: ...
- 通过编写串口助手工具学习MFC过程——(四)添加ComboBox组合框
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- 如何获取图片的base64编码
1.准备一张图片,比如1.gif 2.使用chrome浏览器,新建立一个窗口,然后将a.png拖动至浏览器窗口里面,打开控制台(检查),最后点击source 3.使用方法: 注意source获取的一串 ...