LeetCode 853. Car Fleet

1AC - Yay! A super cute one : ) Again, simulate & play with it in your mind, and you will get it.

Key: for car[i] at its position, it can only pass the target with the slowest car ahead of it. So the problem becomes, count # of `slowest` cars in the list.

class Solution(object):
    def carFleet(self, target, position, speed):
        """
        :type target: int
        :type position: List[int]
        :type speed: List[int]
        :rtype: int
        """
        if len(position) < 2: return len(position)

        # Get a sorted list of (position, time-to-target)
        ts = [(target-v)*1.0/s for (v, s) in zip(position, speed)]
        v = sorted(zip(position, ts))

        # Count the number of slowest fleets
        ret, slowest = 0, -1.0
        for (_, t) in reversed(v):
            if t > slowest:
                slowest = t
                ret += 1
        return ret
        

原文地址:https://www.cnblogs.com/tonix/p/9194820.html

时间: 2024-10-21 01:45:35

LeetCode 853. Car Fleet的相关文章

[LeetCode] 853. Car Fleet 车队

N cars are going to the same destination along a one lane road.  The destination is target miles away. Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road. A car can n

853.&#160;Car Fleet

https://leetcode.com/problems/car-fleet/solution/ https://leetcode.com/problems/car-fleet/discuss/139999/Easy-understanding-JAVA-TreeMap-Solution-with-explanation-and-comment N cars are going to the same destination along a one lane road.  The destin

LeetCode——853.车队

N 辆车沿着一条车道驶向位于 target 英里之外的共同目的地. 每辆车 i 以恒定的速度 speed[i] (英里/小时),从初始位置 position[i] (英里) 沿车道驶向目的地. 一辆车永远不会超过前面的另一辆车,但它可以追上去,并与前车以相同的速度紧接着行驶. 此时,我们会忽略这两辆车之间的距离,也就是说,它们被假定处于相同的位置. 车队 是一些由行驶在相同位置.具有相同速度的车组成的非空集合.注意,一辆车也可以是一个车队. 即便一辆车在目的地才赶上了一个车队,它们仍然会被视作是

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

[Biweekly LeetCode]2. 字符串的索引对 | Index Pairs of a String

原文地址:https://www.cnblogs.com/strengthen/p/10961676.html Given a text string and words (a list of strings), return all index pairs [i, j] so that the substring text[i]...text[j] is in the list of words. Example 1: Input: text = "thestoryofleetcodeandm

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个