Meta Data#
Difficulty: medium First Attempt: 2025-09-21
- Submissions: Accepted (first try)
Approach#
A subarray’s value is its max minus its min. Any subarray’s max is <= the array’s max and its min >= the array’s min, so the whole array has the largest value. The same subarray may be chosen repeatedly, so choose it k times.
My Solution#
class Solution:
def maxTotalValue(self, nums: List[int], k: int) -> int:
return (max(nums) - min(nums)) * k
Complexity#
Time O(n), space O(1).
Mistakes to Avoid#
- Notice the “same subarray can repeat” condition; if each subarray could be chosen only once, this shortcut fails and you would need the
klargest values.
Interview Explanation#
No subarray’s max minus min exceeds the whole array’s, and repeats are allowed, so the answer is the whole array’s value times k. Time O(n).
