Tap here to switch tabs
Problem
Submissions

Problem: Maximize Total Cost of Alternating Subarrays

Maximize Total Cost of Alternating Subarrays asks you to split an array into contiguous parts and return the greatest total cost, with alternating signs in each part, even for negative values.

Statement

You are given an integer array nums.

Consider partitioning nums into one or more contiguous nonempty subarrays. The cost of a subarray nums[l \dots r] is calculated as:

$ ext{cost}(l, r) = ext{nums}[l] - ext{nums}[l + 1] + ext{nums}[l + 2] - \dots + ext{nums}[r] imes (-1)^{r - l}$

The total cost of the partition is the sum of the costs of each subarray.

Return the maximum possible total cost after partitioning nums.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

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

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize Total Cost of Alternating Subarrays

Maximize Total Cost of Alternating Subarrays asks you to split an array into contiguous parts and return the greatest total cost, with alternating signs in each part, even for negative values.

Statement

You are given an integer array nums.

Consider partitioning nums into one or more contiguous nonempty subarrays. The cost of a subarray nums[l \dots r] is calculated as:

$ ext{cost}(l, r) = ext{nums}[l] - ext{nums}[l + 1] + ext{nums}[l + 2] - \dots + ext{nums}[r] imes (-1)^{r - l}$

The total cost of the partition is the sum of the costs of each subarray.

Return the maximum possible total cost after partitioning nums.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

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