Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added carRefueling.cpp #752

Open
wants to merge 4 commits into
base: master
from

Conversation

@darshanhande11
Copy link

@darshanhande11 darshanhande11 commented Sep 1, 2020

Fixes #726

By submitting this pull request I confirm I've read and complied with the below declarations.

  • I have read the Contribution guidelines and I am confident that my PR reflects them.
  • I have followed the coding guidelines for this project.
  • My code follows the skeleton code structure.
  • This pull request has a descriptive title. For example, Added {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • This pull request will be closed if I fail to update it even once in a continuous time span of 7 days.
  • This pull request shall only be reviewed and merged once the Travis build passes. No maintainer or supporter shall be obliged to review it before this condition is met.
  • I have mentioned the issue number correctly (with hyperlink) in this pull request description.
@darshanhande11
Copy link
Author

@darshanhande11 darshanhande11 commented Sep 2, 2020

@iiitv/project-maintainers please review my PR and let me know if any changes are to be done.

Problem Introduction
You are going to travel to another city that is located 𝑑 miles away from your home city. Your car can travel
at most 𝑚 miles on a full tank and you start with a full tank. Along your way, there are gas stations at
distances stop1, stop2, . . . , stop𝑛 from your home city. What is the minimum number of refills needed?

Input Format : The first line contains an integer 𝑑. The second line contains an integer 𝑚. The third line
specifies an integer 𝑛. Finally, the last line contains integers stop1, stop2, . . . , stop𝑛.

Output Format : Assuming that the distance between the cities is 𝑑 miles, a car can travel at most 𝑚 miles
on a full tank, and there are gas stations at distances stop1, stop2, . . . , stop𝑛 along the way, output the
minimum number of refills needed. Assume that the car starts with a full tank. If it is not possible to
reach the destination, output −1.

Output Screenshots :

image
The distance between the cities is 950, the car can travel at most 400 miles on a full tank. It suffices
to make two refills: at points 375 and 750. This is the minimum number of refills as with a single refill
one would only be able to travel at most 800 miles.

image
One cannot reach the gas station at point 9 as the previous gas station is too far away

@darshanhande11
Copy link
Author

@darshanhande11 darshanhande11 commented Sep 30, 2020

@iiitv/project-maintainers Please review my PR and let me know if any changes are to be done.

Copy link

@ishanksoni ishanksoni left a comment

Add a new line at end of file and write time complexity of solution.

Copy link

@ishanksoni ishanksoni left a comment

Correct the time complexity of code, i think it should be O(n).


return 0;
}
//Time Complexity of the implementation : O(n^2)

This comment has been minimized.

@ishanksoni

ishanksoni Oct 1, 2020

@darshanhande11 , It doesn't seem the correct time complexity of your implementation, Please analyse the code again :)

This comment has been minimized.

@darshanhande11

darshanhande11 Oct 1, 2020
Author

@ishanksoni Yes sir, the time complexity is O(n), I overlooked the fact that the nested while loop takes a constant amount of time and the outer while loop takes at most n+1 iterations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.