↓Skip to main content
  1. LeetCode/

LeetCode 3689: Maximum Total Subarray Value I

·1 min· ·
LeetCode Medium Greedy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

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 k largest 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).

Related

LeetCode 2294: Partition Array Such That Maximum Difference Is K
·1 min
LeetCode Medium Greedy Sorting Array
LeetCode 2294 note: subsequences ignore order, so sort and extend each group from its minimum until a value exceeds min + k
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough
LeetCode 3638: Maximum Balanced Shipments
·2 mins
LeetCode Medium Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 3638 note: greedily cut a segment as soon as a value below its maximum appears; until then the segment is non-decreasing, so its maximum is the previous value
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once