【leetcode】1094. Car Pooling
题目如下:
You are driving a vehicle that has
capacityempty 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
trueif 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 <= 1000trips[i].length == 31 <= trips[i][0] <= 1000 <= trips[i][1] < trips[i][2] <= 10001 <= 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 ...
随机推荐
- jenkins打包ios 报错rror: No signing certificate "iOS Distribution" found: No "iOS Distribution...
错误提示如图: error: No signing certificate "iOS Distribution" found: No "iOS Distribution& ...
- 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第3节 综合案例_文件上传_5_综合案例_文件上传案例优化
自定义文件命名 文件名称被写死了 服务器上传了一张图片,服务器就停止了 把服务器端的代码放在while循环里面 服务器也不用 关闭了. 上传完成后服务器端没有关闭 再来启动客户端,又上传一张投片. 多 ...
- http://bbs.csdn.net/topics/340046630 dbgrid怎么获取当前记录值
这根grid无关,当你选中一条记录时,数据集就定位到这条数据上了,你只需要读取数据集中的数据就行了 对我有用[0] 丢个板砖[0] 引用 | 举报| 管理 hongss hongss 本版等级: ...
- 字符串 映射相应的 函数 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke
http://blog.csdn.net/qustdong/article/details/7267258 字符串驱动技术—— MethodAddress , MethodName , ObjectI ...
- spring map获取同类型的bean
今天看博客怎么减少if else 方法, 才发现spring 还有很多功能我没有用到,以后真的得花时间学学spring,今天学到的东西如下: 1.定义一个接口 store public interfa ...
- 流程控制: if分支 while循环 for循环
流程控制 Python程序执行,一定按照某种规律在执行 1.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构 2.遇到需要条件判断选择不同执行路线的执行方式:分支结构 3.有些事情 ...
- IDEA-关闭自动保存&标志修改文件为星号(一)
IDEA优化 intellij 关闭自动保存
- IIS中配置Office COM组件 [dufu图解系列]
解决问题: 读写office文档目前有2中方法,一种为引用微软的office com组件(下图为Microsoft Office 16.0 Object Library), 另一种为用第三方组件NPO ...
- [Git] 002 初识 Git 与 GitHub 之加入文件 第一弹
在 GitHub 的 UI 界面使用 Git 往仓库里加文件 第一弹 1. 点击右上方的 Create new file 2. 在左上方填入文件名,若有后缀,记得加上 3. 页面跳转,此时已有两个文件 ...
- [Web 前端] 011 css 背景属性
1. 概览 参数 释义 background-color 背景颜色 background-image 背景图片 background-repeat 是否重复 background-position 定 ...