↓Skip to main content
  1. LeetCode/

LeetCode 3796: Find Maximum Value in a Constrained Sequence

·2 mins· ·
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: 2026-01-03

  • Submissions: Wrong Answer ×2 → Accepted (2026-01-04)

Approach
#

Let ub[i] be the largest possible a[i]. Three things limit it:

  • Its own restrictions (and a[0] is fixed at 0).
  • The left: a[i] <= a[i - 1] + diff[i - 1], so ub[i] <= ub[i - 1] + diff[i - 1].
  • The right: likewise ub[i] <= ub[i + 1] + diff[i].

Propagate once left to right and once right to left (Greedy Template 3). Afterwards neighboring bounds differ by at most diff, so ub itself is a valid sequence reaching every bound at once, and the answer is max(ub).

My Attempts
#

I first tried binary-searching, between each pair of restricted points, for where the two slopes cross.

  1. Attempt 1 did not propagate the limits: for the last segment it used “last restriction + remaining differences” without checking reachability from a[0] = 0. For n = 2, restrictions = [[1, 3]], diff = [0], a[1] can only be 0; it returned 3.
  2. Attempt 2 propagated the restriction values both ways, but after finding the first position where the left slope exceeds the right, it returned r - 1, checking only the cell before the crossing. For n = 4, restrictions = [[3, 1]], diff = [3, 3, 3] the best is [0, 3, 4, 1] with the maximum 4 at the crossing cell; it returned 3.
  3. Computing a bound for every position with one pass each way passed, with no binary search needed.

Solution
#

class Solution:
    def findMaxVal(self, n: int, restrictions: List[List[int]], diff: List[int]) -> int:
        ub = [inf] * n
        ub[0] = 0
        for idx, cap in restrictions:
            ub[idx] = min(ub[idx], cap)
        for i in range(n - 1):                     # from the left
            ub[i + 1] = min(ub[i + 1], ub[i] + diff[i])
        for i in range(n - 2, -1, -1):             # from the right
            ub[i] = min(ub[i], ub[i + 1] + diff[i])
        return max(ub)

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Limits must propagate in both directions, not just between neighboring restricted points.
  • The peak of two slopes can be the crossing cell itself; do not check only the cell before it.

Interview Explanation
#

Each position’s maximum is limited by its own restriction and by its neighbors. I fill the restrictions into a bound array and propagate once each way; afterwards every neighboring difference is legal, so all bounds are reachable together and the answer is the largest bound. Time O(n).

Related

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
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 334: Increasing Triplet Subsequence
·2 mins
LeetCode Medium Array Greedy Longest-Increasing-Subsequence
LeetCode 334 note: keep the smallest first and the smallest second that has something smaller before it; any number above second completes a triplet