Tap here to switch tabs
Problem
Submissions

Problem: Maximum Total Subarray Value II

med
30 min
Try to solve the Maximum Total Subarray Value II problem.

Statement

You are given an integer array nums of length nn and an integer k.

The value of a contiguous subarray is defined as the difference between its maximum element and its minimum element.

You must select kk distinct subarrays of nums. Return the maximum possible sum of the values of the chosen subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤5imes104\leq 5 imes 10^4

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • 1≤k≤min⁡(105,nimes(n+1)/2)1 \leq k \leq \min(10^5, n imes (n + 1) / 2)

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Total Subarray Value II

med
30 min
Try to solve the Maximum Total Subarray Value II problem.

Statement

You are given an integer array nums of length nn and an integer k.

The value of a contiguous subarray is defined as the difference between its maximum element and its minimum element.

You must select kk distinct subarrays of nums. Return the maximum possible sum of the values of the chosen subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤5imes104\leq 5 imes 10^4

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • 1≤k≤min⁡(105,nimes(n+1)/2)1 \leq k \leq \min(10^5, n imes (n + 1) / 2)