algos icon indicating copy to clipboard operation
algos copied to clipboard

added carRefueling.cpp

Open darshanhande11 opened this issue 5 years ago β€’ 2 comments
trafficstars

Fixes #726

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

  • [x] I have read the Contribution guidelines and I am confident that my PR reflects them.
  • [x] I have followed the coding guidelines for this project.
  • [x] My code follows the skeleton code structure.
  • [x] This pull request has a descriptive title. For example, Added {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • [x] This pull request will be closed if I fail to update it even once in a continuous time span of 7 days.
  • [x] 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.
  • [x] I have mentioned the issue number correctly (with hyperlink) in this pull request description.

darshanhande11 avatar Sep 01 '20 12:09 darshanhande11

@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 avatar Sep 02 '20 06:09 darshanhande11

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

darshanhande11 avatar Sep 30 '20 03:09 darshanhande11