题目如下:

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 tripstrip[i] = [num_passengers, start_location, end_location] contains information about the i-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: false

Example 2:

Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true

Example 3:

Input: trips = [[2,1,5],[3,5,7]], capacity = 3
Output: true

Example 4:

Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11
Output: true
 

Constraints:

  1. trips.length <= 1000
  2. trips[i].length == 3
  3. 1 <= trips[i][0] <= 100
  4. 0 <= trips[i][1] < trips[i][2] <= 1000
  5. 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的更多相关文章

  1. 【LeetCode】1094. Car Pooling 拼车

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 差分数组 代码 日期 题目地址:https://le ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. 【C++进阶:STL常见性质3】

    STL3个代表性函数:for_each(), random_shuffle(), sort() vector<int> stuff; random_shuffle(stuff.begin( ...

  2. KVM 虚拟化架构和实现原理

    目录 目录 KVM虚拟化架构 devkvm QEMU OpenstackKVMQEMU 的关系 KVM的虚拟化实现 KVM虚拟化架构 KVM是嵌入在Linux操作系统标准内核中的一个虚拟化模块,它能够 ...

  3. R语言中的几种数据结构

    R语言中的几种数据结构 一  R中对象的5种基本类型 字符(character) 整数 (integer) 复数(complex) 逻辑(logical:True/False) 数值(numeric: ...

  4. Binder进程与线程ProcessState以及IPCThreadState

    ProcessState以及IPCThreadState ProcessState是负责打开Binder节点并做mmap映射,IPCThreadState是负责与Binder驱动进行具体的命令交互. ...

  5. MIUI8改MAC

    1.手机Root 2.RE文件管理器复制:/data/nvram/APCFG/APRDEB/WIFI 到电脑上 3.如下图用UltraEdit打开WIFI文件修改MAC 4.拷贝修改后的WIFI文件到 ...

  6. linux启动内核源码分析

    内核的启动时从main.c这个文件里面的start_kernel函数开始的,这个文件在linux源码里面的init文件夹下面 下面我们来看看这个函数 这个函数很长,可以看个大概过去 asmlinkag ...

  7. java Iterator Iterable Collection AbstractCollection Map关系

    java.lang Interface Iterable<T>  实现该接口就可以使用for-each循环. java.util Interface Iterator<E>   ...

  8. js之状态模式

    level01:电灯程序 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  9. node.js多版本管理 nvm

    安装nvm 1.nvm压缩包下载地址 https://github.com/coreybutler/nvm-windows/releases 注意:Windows用户下载nvm-setup.zip 2 ...

  10. [mysql相关集锦] 001 - mysql zip安装/The service already exists/MySQL 服务无法启动

    本随笔参考了其他博客内容,且在验证有效之下才或誊抄或摘录或加上自己经验组合而成. 参考博客: 1,https://www.jianshu.com/p/413d29b67422 2,https://bl ...