Car Fleet-II

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 163:-Car Fleet-II

The "Car Fleet II" problem is an extension of the "Car Fleet" problem, where you need to determine the time at which each car fleet will reach the target destination.

Here's how you can approach the "Car Fleet II" problem:

  1. Calculate the time it takes for each car to reach the target based on their positions and speeds. The time can be calculated as time = distance / speed.

  2. Iterate through the cars in reverse order, calculating the time it takes for each car to catch up with the car ahead of it.

  3. Maintain a stack to keep track of cars that are forming a fleet. If the time it takes for the current car to catch up is greater than the time of the car at the top of the stack, a new fleet is formed. Otherwise, update the time of the car at the top of the stack and pop cars from the stack until a valid fleet is formed.

  4. Store the calculated times in a result array and return the result.

Here's a C++ implementation to solve the "Car Fleet II" problem:

 

cppCopy code

#include <iostream> #include <vector> #include <stack> std::vector<double> getCollisionTimes(std::vector<int>& position, std::vector<int>& speed) { int n = position.size(); std::vector<double> collisionTimes(n, -1.0); std::stack<int> stack; for (int i = n - 1; i >= 0; --i) { while (!stack.empty()) { int j = stack.top(); double time = static_cast<double>(position[j] - position[i]) / (speed[i] - speed[j]); if (speed[j] <= speed[i] || (collisionTimes[j] != -1.0 && time >= collisionTimes[j])) { stack.pop(); } else { collisionTimes[i] = time; break; } } stack.push(i); } return collisionTimes; } int main() { std::vector<int> position = {3, 5, 7, 10}; std::vector<int> speed = {5, 3, 7, 10}; std::vector<double> collisionTimes = getCollisionTimes(position, speed); std::cout << "Collision Times: "; for (double time : collisionTimes) { std::cout << time << " "; } std::cout << std::endl; return 0; }

In this example, the getCollisionTimes function calculates the collision times for each car based on the given positions and speeds of the cars. The main function demonstrates how to use this function for a specific input.

For the given input, the output collision times will be:

 

mathematicaCopy code

Collision Times: 2 -1 1.5 -1

This indicates that the first car will collide with the third car at time 2, the third car will collide with the fourth car at time 1.5, and the second and fourth cars will not collide with any other cars.

21. Stacks - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?