Skip to content

Practice Maximize Total Cost of Alternating Subarrays

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3196
LeetCode Problem

Maximize Total Cost of Alternating Subarrays

Problem Statement

Partition nums into one or more nonempty contiguous subarrays, with every element in exactly one part. Each part contributes its alternating sum, starting with a plus sign: nums[l] - nums[l + 1] + nums[l + 2] - ... through nums[r]. Return the maximum sum of these part costs. Keeping the entire array as one part is allowed.

Source: Maximize Total Cost of Alternating Subarrays on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,-2,3,4]
Output
10

# Example 2

Input
nums = [1,-1,1,-1]
Output
4

# Example 3

Input
nums = [0]
Output
0

# Example 4

Input
nums = [1,-1]
Output
2

Constraints

1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Maximize Total Cost of Alternating Subarrays.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.