Skip to main content

Maximum Value of an Alternating Sequence - Video Solutions

MediumMathGreedy

3993. Maximum Value of an Alternating Sequence (Leetcode Medium)

8 video solutions available

Maximum Value of an Alternating Sequence - Video Solution

Watch 8 video solutions for Maximum Value of an Alternating Sequence, a medium level problem involving Math, Greedy. This walkthrough by Programming Live with Larry has 106 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given three integers n, s, and m.

A sequence seq of integers of length n is considered valid if:

  • seq[0] = s.
  • The sequence is alternating, meaning that either:
    • seq[0] > seq[1] < seq[2] > ..., or
    • seq[0] < seq[1] > seq[2] < ....
  • For every adjacent pair, |seq[i] - seq[i - 1]| <= m.

A sequence of length 1 is considered alternating.

Return the maximum possible element that can appear in any valid sequence.

 

Example 1:

Input: n = 4, s = 3, m = 5

Output: 12

Explanation:

  • One valid sequence is [3, 8, 7, 12].
  • The maximum element in the sequence is 12.

Example 2:

Input: n = 2, s = 4, m = 3

Output: 7

Explanation:

  • One valid sequence is [4, 7].
  • The maximum element in the sequence is 7.

 

Constraints:

  • 1 <= n, s <= 109
  • 1 <= m <= 105
Read full problem with examples

Approach Overview

Problem Overview: You need to build an alternating sequence that maximizes the final value while following alternating addition and subtraction rules. The challenge is deciding whether to include the current number and which alternating state it belongs to.

Approach 1: Brute Force Recursion (O(2^n) time, O(n) space)

The direct approach tries every possible subsequence and alternates between adding and subtracting values. At each index, you either skip the number or include it in the current alternating position. This works for understanding the state transition, but the recursion tree grows exponentially. Use this approach only for very small inputs or for deriving the recurrence relation before optimization.

Approach 2: Dynamic Programming with Memoization (O(n) time, O(n) space)

Store the best answer for each index and alternating state using a memo table. The two states usually represent whether the next chosen element should be added or subtracted. Each recursive call performs constant work after memoization, reducing repeated calculations. This is the standard top-down dynamic programming optimization for alternating sequence problems.

Approach 3: Iterative DP State Compression (O(n) time, O(1) space)

The optimal solution tracks only two running values: the best score when the current element is treated as a positive contribution and the best score when it is treated as a negative contribution. Iterate through the array once and update both states using previous values. This removes the DP array entirely and keeps memory constant. Interviewers usually expect this version because it shows you understand DP state transitions and optimization.

Recommended for interviews: Start by explaining the brute force recursion to show the alternating-choice structure. Then move to the compressed DP solution with two states. The O(n) time and O(1) space approach is the strongest answer because it combines clean state modeling with optimal performance. Problems like this also overlap with greedy transition reasoning and sequence optimization patterns.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force RecursionO(2^n)O(n)Useful for deriving recurrence relations or validating small cases
DP with MemoizationO(n)O(n)General solution with clear recursive state transitions
Iterative DP State CompressionO(n)O(1)Best choice for interviews and large input constraints