Leetcode题解
  • README
  • 7.Reverse Integer
  • 9.Palindrome Number
  • 13.Roman to Integer
  • 19.Remove Nth Node from End of List
  • 20.Valid Parentheses
  • 21.Merge Two Sorted Lists
  • 22.Generate Parentheses
  • 26.Remove Duplicates from Sorted Array
  • 27.Remove Element
  • 28.Implement strStr()
  • 35.Search Insert Position
  • 38.Count and Say
  • 46.Permutations
  • 53.Maximum Subarray
  • 58.Length of Last Word
  • 59.Spiral Matrix II
  • 66.Plus One
  • 67.Add Binary
  • 69.Sqrt
  • 70.Climbing Stairs
  • 77.Combinations
  • 78.Subsets
  • 83.Remove Duplicates from Sorted List
  • 88.Merge Sorted Array
  • 104.Maximum Depth of Binary Tree
  • 107.Binary Tree Level Order Traversal II
  • 108.Convert Sorted Array to Binary Search Tree
  • 100.Same Tree
  • 101.Symmetric Tree
  • 105.Construct Binary Tree from Preorder and Inorder Traversal
  • 110.Balanced Binary Tree
  • 111.Minimum Depth of Binary Tree
  • 112.Path Sum
  • 118.Pascals Triangle
  • 119.Pascals Triangle II
  • 121.Best Time to Buy and Sell Stock
  • 125.Valid Palindrome
  • 131.Palindrome Partitioning
  • 139.Word Break
  • 140.Word Break II
  • 141.Linked List Cycle
  • 143.Reorder List
  • 155.Min Stack
  • 167.Two Sum II- Input Array is Sorted
  • 168.Excel Sheet Column Title
  • 169.Majority Element
  • 171.Excel Sheet Column Number
  • 172.Factorial Trailing Zeroes
  • 175.Combine Two Tables
  • 181.Employee Earning More Than Their Managers
  • 182.Duplicate Emails
  • 183.Customers Who Never Order
  • 189.Rotate Array
  • 191.Number of 1 Bits
  • 193.Valid Phone Numbers
  • 195.Tenth Line
  • 196.Delete Duplicate Emails
  • 197.Rising Temperature
  • 198.House Robber
  • 202.Happy Number
  • 203.Remove Linked List Elements
  • 204.Count Primes
  • 205.Isomorphic Strings
  • 206.Reverse Linked List
  • 217.Contains Duplicate
  • 219.Contains Duplicate II
  • 225.Implement Stack Using Queues
  • 226.Invert Binary Tree
  • 231.Power of Two
  • 232.Implement Queue Using Stack
  • 234.Palindrome Linked List
  • 237.Delete Node in a Linked List
  • 240.Search a 2D Matrix II
  • 242.Valid Anagram
  • 257.Binary Tree Paths
  • 258.Add Digits
  • 263.Ugly Numbers
  • 268.Missing Number
  • 278.First Bad Version
  • 284.Peeking Iterator
  • 290.Word Pattern
  • 282.Nim Game
  • 303.Range Sum Query Immutable
  • 338.Counting Bits
  • 342.Power of Four
  • 344.Reverse String
  • 345.Reverse Vowels of a String
  • 349.Intersection of Two Arrays
  • 350.Intersection of Two Arrays II
  • 367.Valid Perfect Square
  • 371.Sum of Two Integers
  • 374.Guess Number Higher or Lower
  • 383.Ransom Note
  • 387.First Unique Character in a String
  • 400.Nth Digit
  • 401.Binary Watch
  • 404.Sum of Left Leaves
  • 405.Convert A Number to Hexadecimal
  • 409.Longest Palindrome
  • 412.Fizz Buzz
  • 414.Third Maximum Number
  • 415.Add Strings
  • 427.Construct Quad Tree
  • 429.N Ary Tree Level Order Traversal
  • 434.Number of Segments in a String
  • 437.Path Sum III
  • 438.Find All Anagrams in a String
  • 441.Arranging Coins
  • 443.String Compression
  • 447.Number of Boomerangs
  • 448.Find All Numbers Disappeared in an Array
  • 453.Minimum Moves to Equal Array Elements
  • 455.Assign Cookies
  • 459.Repeated Substring Pattern
  • 461.Hamming Distance
  • 463.Island Perimeter
  • 475.Heaters
  • 476.Number Complement
  • 482.License Key Formatting
  • 485.Max Consecutive Ones
  • 492.Construct the Rectangle
  • 496.Next Greater Element I
  • 500.Keyboard Row
  • 501.Find Mode in Binary Tree
  • 504.Base 7
  • 506.Relative Ranks
  • 507.Perfect Number
  • 509.Fibonacci Number
  • 520.Detect Capital
  • 521.Longest Uncommon Subsequence I
  • 530.Minimum Absolute Difference in BST
  • 532.K-diff Pairs in an Array
  • 535.Encode and Decode TinyURL
  • 538.Convert BST to Greater Tree
  • 541.Reverse String II
  • 543.Diameter of Binary Tree
  • 551.Student Attendace Record I
  • 557.Reverse Words in a String
  • 558.Quad Tree Intersection
  • 559.Maxmium Depth of N Ary Tree
  • 561.Array Partition I
  • 563.Binary Tree Tilt
  • 566.Reshape the Matrix
  • 572.Subtree of Another Tree
  • 575.Distribute Candies
  • 581.Shortest Unsorted Continuous Subarray
  • 589.N Ary Tree Preorder Traversal
  • 590.N Ary Tree Postorder Traversal
  • 594.Longest Harmonious Subsequence
  • 595.Big Countires
  • 596.Classes More Than 5 Students
  • 598.Range Addition II
  • 599.Minimum Index of Two Lists
  • 605.Can Place Flowers
  • 606.Construct String from Binary Tree
  • 617.Merge Two Binary Trees
  • 620.Not Boring Movies
  • 627.Swap Salary
  • 628.Maximum Product of Three Numbers
  • 633.Sum of Square Numbers
  • 637.Average of Levels in Binary Tree
  • 643.Maximum Average Subarray I
  • 645.Set Mismatch
  • 653.Two Sum IV - Input is a BST
  • 654.Maximum Binary Tree
  • 657.Robot Return to Origin
  • 661.Image Smoother
  • 665.Non-Decreasing Array
  • 669.Trim a Binary Search Tree
  • 671.Second Minimum Node in a Binary Tree
  • 680.Valid Palindrome II
  • 682.Baseball Game
  • 686.Repeated String Match
  • 687.Longest Univalue Path
  • 690.Employee Importance
  • 693.Binary Number with Alternating Bits
  • 696.Count Binary Substring
  • 697.Degree of an Array
  • 700.Search in a Binary Search Tree
  • 701.Insert Into a Binary Search Tree
  • 703.Kth Largest Element in a Stream
  • 704.Binary Search
  • 705.Design Hashset
  • 706.Design Hashmap
  • 707.Design Linked List
  • 709.To Lower Case
  • 713.Subarray Product Less Than K
  • 717.1 Bit and 2 Bit Characters
  • 720.Longest Word in Dictionary
  • 724.Find Pivot Index
  • 728.Self Dividing Numbers
  • 733.Flood Fill
  • 739.每日温度
  • 744.Find Smallest Letter Greater Than Target
  • 746.Min Cost Climbing Stairs
  • 747.Largest Number At Least Twice Of Others
  • 748.Shortest Completing Word
  • 754.Reach A Number
  • 762.Prime Number of Set Bits in Binary Representation
  • 766.Toeplitz Matrix
  • 771.Jewels and Stones
  • 783.Minimum Distance Between BST Nodes
  • 784.Letter Case Permutation
  • 788.Rotated Digits
  • 796.Rotate String
  • 804.Unique Morse Code Words
  • 806.Number of Lines to Write String
  • 807.Max Increase to Keep City Skyline
  • 811.Subdomain Visit Count
  • 812.Largest Triangle Area
  • 814.Binary Tree Pruning
  • 819.Most Common Word
  • 821.Shortest Distance to a Character
  • 824.Goat Latin
  • 829.Consecutive Numbers Sum
  • 830.Position of Large Groups
  • 831.Masking Personal Information
  • 832,Flipping an Image
  • 833.Find And Replace in String
  • 835,Image Overlap
  • 836.Rectangle Overlap
  • 840.Magic Squares In Grid
  • 844.Backspace String Compare
  • 848.Shifting Letters
  • 849.Maximize Distance to Closest Person
  • 852.Peak Index in A Mountain Array
  • 853.Car Fleet
  • 855.Exam Room
  • 859.Buddy Strings
  • 860.Lemonade Change
  • 861.Score After Flipping Matrix
  • 864.Shortest Path to Get All Keys
  • 865.Smallest Subtree with all the Deepest Nodes
  • 866.Prime Palindrome
  • 867.transpose
  • 868.Binary Gap
  • 872.Leaf Similar Trees
  • 874.Walking Robot Simulation
  • 876.Middle of the Linked List
  • 883.Projection Area of 3D Shapes
  • 884.Uncommon Words from Two Sentences
  • 887.Super Egg Drop
  • 888.Fair Candy Swap
  • 892.Surface Area of 3D Shapes
  • 893.Groups of Special Equivalent Strings
  • 894.All Possible Full Binary Trees
  • 896.monotonic array
  • 897.increasing order search tree
  • 900.RLE Iterator
  • 901.Online Stock Span
  • 905.Sort Array by Parity
  • 908.Smallest Range I
  • 910.Smallest Range II
  • 913.Cat and Mouse
  • 914.X of a Kind in a Deck of Cards
  • 915.Partition Array into Disjoint Intervals
  • 916.Word Subsets
  • 917.Reverse Only Letters
  • 922.Sort Array by Parity II
  • 925.Long Pressed Name
  • 929.Unique Email Address
  • 937.Reorder Log Files
  • 938.Range Sum of BST
  • 941.Valid Mountain Array
  • 942.Di String Match
  • 944.Delete Column to Make Sorted
  • 949.Largest Time For Given Digits
  • 950.Reveal Cards in Increasing Order
  • 953.Verifying an Alien Dictionary
  • 961.N Repeated Element in Size 2N Array
  • 965.Univalued Binary Tree
  • 970.Powerful Integers
  • 974.Subarray Sums Divisible By K
  • 976.Largest Perimeter Triangle
  • 977.Squares of a Sorted Array
  • 985.Sum of Even Numbers After Queries
  • 989.Add to Array-Form of Integer
  • 990.Satisfiability of Equality Equations
  • 993.Cousins in Binary Tree
  • 994.Rotting Oranges
  • 997.Find the Town Judge
  • 999.Available Captures for Rook
  • 1002.Find Common Characters
  • 1005.Maximise Sum of Array after K Negations
  • 1008.Construct Binary Search Tree from Preorder Traversal
  • 1021.Remove Outermost Parentheses
  • 1025.Divisor Game
  • 1029.Tow City Scheduling
  • 1030.Matrix Cells in Distance Order
  • 1033.Moving Stones Until Consecutive
  • 1037.Valid Boomerang
  • 1038.Binary Search Tree to Greater Sum Tree
  • 1041.Robot Bounded in Circle
  • 1042.Flower Planting with no Adjacent
  • 1046.Last Stone Weight
  • 1089.Duplicate Zeros
  • 1013.Partition Array Into Three Parts with Equal Sum
  • 1018.Binary Prefix Divisible By 5
  • 1022.Sum of Root To Leaf Binary Numbers
  • 1071.Greatest Common Divisor of Strings
  • 1079.Letter Tile Possibilities
  • 1103.Distribute Candies to People
  • 1114.Print in Order
  • 1115.Print FooBar Alternately
  • 1116.Print Zero Even Odd
  • 1117.Building H2O
  • 1128.Number of Equivalent Domino Pairs
  • 1169.Invalid Transactions
  • 1176.Diet Plan Performance
  • 1282.用户分组
  • 1329.Sort the Matrix Diagonally
  • 1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree
  • 1395.Count Number of Teams
  • ByteDance
  • 面试题 64
  • 面试题 16.01
  • 面试题 16.18
  • 面试题 29
  • 面试题 46
Powered by GitBook
On this page

Was this helpful?

853.Car Fleet

853.Car Fleet

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

示例:

输入:target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
输出:3
解释:
从 10 和 8 开始的车会组成一个车队,它们在 12 处相遇。
从 0 处开始的车无法追上其它车,所以它自己就是一个车队。
从 5 和 3 开始的车会组成一个车队,它们在 6 处相遇。
请注意,在到达目的地之前没有其它车会遇到这些车队,所以答案是 3。

提示:

0 <= N <= 10 ^ 4
0 < target <= 10 ^ 6
0 < speed[i] <= 10 ^ 6
0 <= position[i] < target
所有车的初始位置各不相同。

方法: 一辆车追上另一辆车的状况是,后面的车距离目标更远,但到达目的时间不超过前面的车。

  • 理论情况是所有车都在匀速行驶,但如果前面的车追上了更前面的车,那么前面的车速度就会变。此时追上的车可以视为消失了,那么只需要比较后面的车与前前面的车。以此类推。

  • 从最末尾一辆车计算,初始总车队即为车数,随着追上逐渐减少。

  • 创建了一个向量保存车队到达目的地时间。创建了一个函数,用来排序车队和相应到达时间。

  • 然后从末尾车开始比较,如果追上了前面的车,则到达时间也与前面的车相等,车队总数减1,遍历一边车队。

  • 得到最终结果。

class Solution {
public:
    int carFleet(int target, vector<int>& position, vector<int>& speed) {
        int N=position.size();
        if(!N) return 0;
        vector<float>time(N,0);
        for(int i=0;i<N;i++)
            time[i]=(target-position[i]+0.0)/speed[i];
        ordered(position,time);
        int result=N;
        for(int i=N-1;i>0;i--)
        {
            if(time[i] >= time[i-1])
            {
                time[i-1]=time[i];
                result--;
        }
        }
        return result;

    }
private:
    int ordered(vector<int> &position,vector<float> &time)
    {
        int N=position.size();
        for(int i=0 ; i<N;i++)
            for(int j=i+1;j<N;j++)
            {
                float temp;
                int tempn;
                if(position[i]>position[j])
                {
                    tempn=position[i];
                    position[i]=position[j];
                    position[j]=tempn;
                    temp=time[i];
                    time[i]=time[j];
                    time[j]=temp;
                }
            }
        return 0;
    }
};
Previous852.Peak Index in A Mountain ArrayNext855.Exam Room

Last updated 3 years ago

Was this helpful?