GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/cpp/1001-10000/1771-1780/Car Fleet II.cpp at master · gzc/leetcode

Expand file tree

/

Car Fleet II.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
29 lines (25 loc) · 1009 Bytes
/

Car Fleet II.cpp

Copy path

File metadata and controls

29 lines (25 loc) · 1009 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<double> getCollisionTimes(vector<vector<int>>& cars) {
vector<double> result(cars.size(), -1.0);
stack<int> mystack;
for (int i = cars.size() - 1; i >= 0; i--) {
// We don't need faster cars behind current car.
while (!mystack.empty() && cars[i][1] <= cars[mystack.top()][1]) {
mystack.pop();
}
while (!mystack.empty()) {
int candidate = mystack.top();
double current_time = 1.0 * (cars[candidate][0] - cars[i][0]) / (cars[i][1] - cars[candidate][1]);
if (current_time <= result[candidate] || result[candidate] < 0) {
result[i] = current_time;
break;
}
// This car will collide with cars after candidate.
mystack.pop();
}
mystack.push(i);
}
return result;
}
};
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.