3796. Find Maximum Value in a Constrained Sequence
You are given an integer
n, a 2D integer arrayrestrictions, and an integer arraydiffof lengthn - 1. Your task is to construct a sequence of lengthn, denoted bya[0], a[1], ..., a[n - 1], such that it satisfies the following conditions:
a[0]is 0.- All elements in the sequence are non-negative.
- For every index
i(0 <= i <= n - 2),abs(a[i] - a[i + 1]) <= diff[i].- For each
restrictions[i] = [idx, maxVal], the value at positionidxin the sequence must not exceedmaxVal(i.e.,a[idx] <= maxVal).Your goal is to construct a valid sequence that maximizes the largest value within the sequence while satisfying all the above conditions.
Return an integer denoting the largest value present in such an optimal sequence.
1 |
|