Maximize total catch over K hours from ponds whose yield drops after each hour
Company: ByteDance
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Given the current hourly catch of each fishing pond and a budget of K hours, where every hour fished at a pond lowers its catch by one, compute the maximum total number of fish. It tests modeling the declining yields, choosing an optimal schedule, and meeting a time bound better than O(K log N) when K is very large.
Read the full ByteDance Machine Learning Engineer interview experience this question came from
Constraints
- N == len(M) and 1 <= N <= 10^5
- 0 <= M[i] <= 10^6
- 0 <= K <= 10^9
- The answer is at most 10^15, which exceeds 2^31 - 1; use 64-bit integers outside Python (Java long, C++ long long)
- Required running time: asymptotically better than O(K log N)
Examples
Input: ([90, 100], 100)
Expected Output: 7075
Explanation: Source example 1: the taller pond is drained alone from 100 to 91, then both ponds share levels down to 46 (4015 + 3060).
Input: ([3, 5], 4)
Expected Output: 15
Explanation: Source example 2: 5 + 4 + 3 + 3; K exactly equals the number of catches >= 3.
Hints
- Only how many hours you spend at each pond matters, not the order: each pond contributes its first few hourly catches M[i], M[i] - 1, ..., stopping at 0.
- Treat every hour you could ever spend anywhere as a single value. Which K of those values does an optimal plan collect?
- K can reach 10^9, so look for a way to count and sum many hours at once instead of handling them one by one, and keep the total in 64 bits.