Minimum Skips to Arrive at Meeting On Time solution leetcode

Minimum Skips to Arrive at Meeting On Time solution leetcode

You are given an integer hoursBefore, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through n roads. The road lengths are given as an integer array dist of length n, where dist[i] describes the length of the ith road in kilometers. In addition, you are given an integer speed, which is the speed (in km/h) you will travel at.

After you travel road i, you must rest and wait for the next integer hour before you can begin traveling on the next road. Note that you do not have to rest after traveling the last road because you are already at the meeting.

  • For example, if traveling a road takes 1.4 hours, you must wait until the 2 hour mark before traveling the next road. If traveling a road takes exactly 2 hours, you do not need to wait.

However, you are allowed to skip some rests to be able to arrive on time, meaning you do not need to wait for the next integer hour. Note that this means you may finish traveling future roads at different hour marks.

  • For example, suppose traveling the first road takes 1.4 hours and traveling the second road takes 0.6 hours. Skipping the rest after the first road will mean you finish traveling the second road right at the 2 hour mark, letting you start traveling the third road immediately.

Return the minimum number of skips required to arrive at the meeting on time, or -1 if it is impossible.

Minimum Skips to Arrive at Meeting On Time solution leetcode

Example 1: Minimum Skips to Arrive at Meeting On Time solution leetcode

Input: dist = [1,3,2], speed = 4, hoursBefore = 2
Output: 1
Explanation:
Without skipping any rests, you will arrive in (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 hours.
You can skip the first rest to arrive in ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 hours.
Note that the second rest is shortened because you finish traveling the second road at an integer hour due to skipping the first rest.

Example 2: Minimum Skips to Arrive at Meeting On Time solution leetcode

Input: dist = [7,3,5,5], speed = 2, hoursBefore = 10
Output: 2
Explanation:
Without skipping any rests, you will arrive in (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 hours.
You can skip the first and third rest to arrive in ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 hours.

Example 3: Minimum Skips to Arrive at Meeting On Time solution leetcode

Input: dist = [7,3,5,5], speed = 1, hoursBefore = 10
Output: -1
Explanation: It is impossible to arrive at the meeting on time even if you skip all the rests.

Minimum Skips to Arrive at Meeting On Time solution leetcode

Constraints: Minimum Skips to Arrive at Meeting On Time solution leetcode

  • n == dist.length
  • 1 <= n <= 1000
  • 1 <= dist[i] <= 105
  • 1 <= speed <= 106
  • 1 <= hoursBefore <= 107

SOLUTION

Click here



Minimum Skips to Arrive at Meeting On Time solution leetcode Splitting a String Into Descending Consecutive Values Minimum Adjacent Swaps to Reach the Kth Smallest Number Minimum Interval to Include Each Query

Welcome to the 243th LeetCode Weekly Contest

  Important Note

    1. To provide a better contest and ensure fairness, we listened to LeetCoders’ feedback and put in lots of thoughts behind the updated contest rule. Please check out our new contest rule which covers more scenarios with details explained.
    2. The penalty time of 5 minutes will be applied for each wrong submission.
    3. To ensure the fairness of the contest, LeetCode will hide some test cases during the contest. When users submit incorrect submissions, LeetCode will not show the hidden test cases to the users.
    4. The final rating of this contest will be updated within 5 working days after the contest.

Below actions are deemed contest violations

    1. :

      • One user submitting with multiple accounts during a contest. LCUS (leetcode.com) account and LCCN (leetcode-cn.com) account are considered to be separate accounts, even if both accounts belong to the same user.
      • Multiple accounts submitting similar code for the same problem.
      • Creating unwanted disturbances which interrupt other users’ participation in a contest.
      • Disclosing contest solutions in public discuss posts before the end of a contest.

    1. LeetCode heavily emphasizes on the justice and fairness of our contests. We have absolutely

ZERO TOLERANCE

    1.  for violation behaviors (such as plagiarism, cheating, etc). When a user is deemed violating contest rules, we will apply the following penalties on this user:

      • First violation: LeetCoin amount resets to zero and a contest and discuss ban for 1 month.
      • Second violation: Permanent account deactivation without appeal.

    Furthermore, we encourage all participants to contribute to maintaining the justice and fairness of our contest. Users who submit valid violation report(s) will earn additional LeetCoins:

    • For each violating participant, the first 10 users who submit the violation report towards this participant will each earn 20 LeetCoins.
    • Each user can earn up to 100 LeetCoins for reporting violations in a contest.
    • Users will not be rewarded LeetCoins for reports on LCCN users.
 

Leave a Comment