【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 ...
随机推荐
- Selenium学习之==>18种定位方式的使用
Selenium的定位方式一共有18种,单数8种,复数8种,最后两种是前面这16种的底层封装 单数形式8种 # 1.id定位 al = driver.find_element_by_id('i1') ...
- 终极Shell - Oh My Zsh
介绍 zsh: 与 bash 同为 shell 软件,适用于 linux 和 mac,mac 与百度开发机已自带. oh-my-zsh:zsh 的一个开源配置方案,即下即用,免去复杂的配置过程.配置后 ...
- mount挂载相关指令
最近需要重新挂载一块数据盘,增加挂载设置,遇到一些问题做下记录. step1:df -h 或 lsblk 查看分区挂载和对应挂载的目录 /dev/xxx /data step2:umount /dev ...
- django连接数据库的类型
字段类型 django的models里面字段类型除了上面的常用的 models.CharField和models.IntegerField,还有更多的类型 1.models.AutoField 自增列 ...
- 【ABAP系列】SAP ABAP 运算符
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 运算符 前 ...
- HDU4372(第一类斯特林数)
题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 0 < N, F, B <= 2000 首先我们知道一个结论:n的环排列的 ...
- pandas中.value_counts()的用法
原文链接:https://www.jianshu.com/p/f773b4b82c66 value_counts()是一种查看表格某列中有多少个不同值的快捷方法,并计算每个不同值有在该列中有多少重复值 ...
- base64 换表 解密脚本
做逆向经常遇到换表的base64 有了py脚本 一切都好说: import base64 import string str1 = "x2dtJEOmyjacxDemx2eczT5cVS9f ...
- CentOS7 内核模块管理
1.查看所有模块:lsmod 2.查看指定模块的详细信息:modinfo 模块名 3.动态加载模块:modprobe 模块名 4.动态卸载模块:modprobe -r 模块名 5.开机自动加载模块:假 ...
- java8----Predicate接口的使用
//5.lambda表达式中加入Predicate // 甚至可以用and().or()和xor()逻辑函数来合并Predicate, // 例如要找到所有以J开始,长度为四个字母的名字,你可以合并两 ...