【leetcode】1094. Car Pooling
题目如下:
You are driving a vehicle that has
capacity
empty seats initially available for passengers. The vehicle only drives east (ie. it cannot turn around and drive west.)Given a list of
trips
,trip[i] = [num_passengers, start_location, end_location]
contains information about thei
-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off. The locations are given as the number of kilometers due east from your vehicle's initial location.Return
true
if and only if it is possible to pick up and drop off all passengers for all the given trips.Example 1:
Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: falseExample 2:
Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: trueExample 3:
Input: trips = [[2,1,5],[3,5,7]], capacity = 3
Output: trueExample 4:
Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
Output: trueConstraints:
trips.length <= 1000
trips[i].length == 3
1 <= trips[i][0] <= 100
0 <= trips[i][1] < trips[i][2] <= 1000
1 <= capacity <= 100000
解题思路:因为 0 <= trips[i][1] < trips[i][2] <= 1000,所以算法为O(n^2)应该是可以接受的。我的方法就是把每个location的人数都算出来,如果任意一个location的人数大于capacity即为false,否则是true。
代码如下:
class Solution(object):
def carPooling(self, trips, capacity):
"""
:type trips: List[List[int]]
:type capacity: int
:rtype: bool
"""
max_des = 0
for i,j,k in trips:
max_des = max(max_des,k)
val = [0] * (max_des + 1)
for i, j, k in trips:
for d in range(j,k):
val[d] += i
if val[d] > capacity:
return False
return True
【leetcode】1094. Car Pooling的更多相关文章
- 【LeetCode】1094. Car Pooling 拼车
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 差分数组 代码 日期 题目地址:https://le ...
- 【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 ...
随机推荐
- mysql5.7多实例安装
[root@vhost1]# cd /opt/source[root@vhost1]#ls mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz[root@vhost1 ...
- iptables List the rules in a chain or all chains
[root@e ~]# iptables -hiptables v1.4.21 Usage: iptables -[ACD] chain rule-specification [options] ip ...
- 部署-GPS授时系统:GPS授时系统
ylbtech-部署-GPS授时系统:GPS授时系统 GPS授时系统是针对自动化系统中的计算机.控制装置等进行校时的高科技产品,GPS授时产品它从GPS卫星上获取标准的时间信号,将这些信息通过各种接口 ...
- drop_duplicates()函数
1dataframe删除某一列的重复元素,默认只留下第一次出现的 inplace参数设置为true时直接在原数据上修改,为False时,生成副本. 注意所有函数中inplace一旦设置为True,此时 ...
- free pascal
https://freepascal.org/ free pascal OPEN SOURCE COMPILER FOR PASCAL AND OBJECT PASCAL GENERAL HomeNe ...
- Java 基础-基本数据类型与表达式
基本数据类型 基本概念 标识符 标识符与内存中的某个位置对应,Java 中标识符的规范如下: 必须由大小写字母.下划线.美元符号.数字组成 首字母只能是大小写字母.下划线.美元符号 变量 变量的值可以 ...
- selenium验证码处理之cookie登录
在实际测试中会经常见到登录操作需要验证码验证登录 常见验证有以下几种: 验证码登录 图片识别 图片滑块识别验证 4.简单验证码计算 针对上面的登录验证解决办法有以下几种: 1.让开发去掉验证码 ...
- [开发技巧]·如何让离线安装Python包
[开发技巧]·如何让离线安装Python包 1.问题描述 PyPI(Python Package Index)是python官方的第三方库的仓库,所有人都可以下载第三方库或上传自己开发的库到PyPI. ...
- Implement Queue using Stacks(用两个栈实现队列)
来源:https://leetcode.com/problems/implement-queue-using-stacks Implement the following operations of ...
- Hibernate入门4
HIbernate的导航查询: 适用场景:当一张A表关联到另一张B表的多条记录,存在一对多的关系(或者多对多),那么查询A表的记录时,就可以将A表某条记录关联的B表的所有记录查询出来,这种方式,就叫做 ...