【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 ...
随机推荐
- Linux_用户权限管理
目录 目录 用户管理 useradd创建用户 userdel删除用户 usermod修改用户账号 passwd修改用户密码 用户权限设置 用户组管理 查看用户的属组 修改用户组gpasswd 为没有家 ...
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第3节 maven标准目录结构和常用命令_07maven常用命令
以给的hellowordl的的代码为例. src/main/java下,这是主业务逻辑代码 里面的内容只有一个jsp的跳转 测试包下: 里面很简单的就输出了一句话 复制项目的目录 先cd进入复制的这个 ...
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第1节 基本概念_01maven概述
- spss中如何处理极端值、错误值
spss中如何处理极端值.错误值 spss中录入数据以后,第一步不是去分析数据,而是要检验数据是不是有录入错误的,是不是有不合常理的数据,今天我们要做一个描述性统计,进而查看哪些数据是不合理的.下面是 ...
- C语言的指针和数组
指针和内存 指针变量也是个变量,不过保存的是另一个变量的地址.另外编译器还会记住指针所指向变量的类型,从而在指针运算时根据变量类型采取不同操作. 例如,char * a 定义了char 类型的指针变量 ...
- 正则表达式断言(Assertions)
一 零宽正向先行断言 x(?=y) 仅匹配被y跟随的x. const regExp = /Jack(?=Sparrow|Dawson)/g; const str = 'JackJones JackSp ...
- 常用获取Android崩溃日志和IOS崩溃日志的几种方法
一:前言 在日常测试app时,经常会遇到崩溃问题,测试快速抓取到崩溃日志可以有效方便开发进行定位,快速解决问题所在测试做到测试分析,定位是非常重要的,这也是判断一个测试能力指标的一大维度. 二:And ...
- 【ABAP系列】SAP ABAP POPUP_TO_CONFIRM 弹出框总结
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP POPUP_T ...
- docker安装mysql(Baas)
Docker安装mysql 5.7版本 //拉取mysql镜像 docker pull mysql:5.7 下载完成后,在本地镜像列表里查到REPOSITORY为mysql,标签为5.7的镜像. do ...
- ---Mock---基本使用
一.mock解决的问题 开发时,后端还没完成数据输出,前端只好写静态模拟数据.数据太长了,将数据写在js文件里,完成后挨个改url.某些逻辑复杂的代码,加入或去除模拟数据时得小心翼翼.想要尽可能还原真 ...