Maximum Boat Distance with Resting Energy
Updated
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[]) → longExamples
Example 1
k = 2wind = [5,1,10]return = 15Moving on days one and three after resting on day two yields fifteen.
Example 2
k = 1wind = [-5,-2]return = 0Rest every day for zero distance.
Example 3
k = 0wind = [4,5]return = 0With zero energy the boat cannot move.
Example 4
k = 3wind = [20,10,2,5,50,15,13]return = 108Seven days with these values and 3 energy. Six moves are impossible: they need 6 energy, while the start gives 3 and the single rest day adds at most 1. Five moves are possible: move on days 1 and 2 (energy 3 to 1), rest on days 3 and 4 (back to 3), then move on days 5, 6 and 7. That uses the five largest values, 20 + 10 + 50 + 15 + 13 = 108.
Constraints
0 <= k < 1000.0 <= wind.length <= 100000.- The answer fits in a signed 64-bit integer.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Maximum Value from Circular HousesDigitalOcean · ONSITE INTERVIEWDigitalOcean interview questions