FastPrepMaximum Boat Distance with Resting Energy

Maximum Boat Distance with Resting Energy

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A boat starts with energy k. On day i, it may move only with positive energy, gaining wind[i] distance and losing one energy, or rest, gaining one energy up to the cap k.

Wind may be negative. Return the maximum total distance after all days without allowing energy below zero. Resting is always allowed, so the answer is at least zero.

Function

maximumBoatDistance(k: int, wind: int[]) → long

Examples

Example 1

k = 2wind = [5,1,10]return = 15

Moving on days one and three after resting on day two yields fifteen.

Example 2

k = 1wind = [-5,-2]return = 0

Rest every day for zero distance.

Example 3

k = 0wind = [4,5]return = 0

With zero energy the boat cannot move.

Constraints

  • 0 <= k < 1000.
  • 0 <= wind.length <= 100000.
  • The answer fits in a signed 64-bit integer.

More Google problems

See Google hiring insights
public long maximumBoatDistance(int k, int[] wind) {
    // write your code here
}
k2
wind[5,1,10]
expected15
Checking account…