FastPrepMaximize Fish over Limited Hours

Maximize Fish over Limited Hours

ByteDance logoByteDance● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given initial hourly fish yields yields for several ponds and a number of fishing hours hours. In each hour, choose exactly one pond, collect its current yield, and decrease that pond's future yield by one without going below zero.

Return the maximum total fish that can be collected. Your solution should aggregate yield levels instead of simulating every hour with a heap.

Function

maximizeFish(yields: int[], hours: long) → long

Examples

Example 1

yields = [90,100]hours = 100return = 7075

The optimal schedule always takes a currently largest yield; aggregating the top one hundred available positive yields gives 7075.

Example 2

yields = [2,1]hours = 5return = 4

The positive collections are 2, 1, and 1; remaining hours contribute zero.

Constraints

  • 1 <= yields.length <= 200000
  • 0 <= yields[i] <= 1000000000
  • 1 <= hours <= 100000000000000
  • The answer fits signed 64-bit arithmetic.

More ByteDance problems

See ByteDance hiring insights
public long maximizeFish(int[] yields, long hours) {
    // Write your code here.
}
yields[90,100]
hours100
expected7075
Checking account…