GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/cpp/1001-10000/1771-1780/Car Fleet II.cpp at master · gzc/leetcode Expand file tree/ Copy pathCar Fleet II.cppMore file actions 29 lines (25 loc) · 1009 Bytes / Copy pathCar Fleet II.cppFile metadata and controls29 lines (25 loc) · 1009 Bytes 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. |