Minimize the Maximum Adjacent Element Difference Problem
Description
LeetCode Problem 3357.
You are given an array of integers nums. Some values in nums are missing and are denoted by -1.
You must choose a pair of positive integers (x, y) exactly once and replace each missing element with either x or y.
You need to minimize the maximum absolute difference between adjacent elements of nums after replacements.
Return the minimum possible difference.
Example 1:
Input: nums = [1,2,-1,10,8]
Output: 4
Explanation:
By choosing the pair as (6, 7), nums can be changed to [1, 2, 6, 10, 8].
The absolute differences between adjacent elements are:
- |1 - 2| == 1
- |2 - 6| == 4
- |6 - 10| == 4
- |10 - 8| == 2
Example 2:
Input: nums = [-1,-1,-1]
Output: 0
Explanation:
By choosing the pair as (4, 4), nums can be changed to [4, 4, 4].
Example 3:
Input: nums = [-1,10,-1,8]
Output: 1
Explanation:
By choosing the pair as (11, 9), nums can be changed to [11, 10, 9, 8].
Constraints:
2 <= nums.length <= 10^5nums[i]is either -1 or in the range[1, 10^9].
Solution
Approach: Binary Search on the Answer
Differences between two neighbors that are both known cannot be changed, so they give a lower bound base. Every -1 is replaced by one of two chosen values x < y (using more than two distinct values never helps).
Consider the known numbers that sit next to a run of -1. Let lo and hi be the smallest and largest of them. For a candidate maximum difference d, the best choice is x = lo + d and y = hi - d (and at least 1), since x has to be reachable from the large values and y from the small ones.
For each run of -1 with optional known ends a and b, try all assignments of its first and last cell to x or y, and check that the ends are within d of a and b, that the two end values are within d of each other, and that a run of length 1 uses a single value. If every run can be filled, d is feasible. Feasibility is monotone in d, so binary search from base up to 10^9 finds the smallest feasible d.
Complexity: O(n log M) time and O(n) space, where M is the largest value.
class Solution:
def minDifference(self, nums: List[int]) -> int:
n = len(nums)
base = 0 # difference between neighbours that are both known
lo, hi = float("inf"), 0 # smallest / largest known value next to a -1
runs = [] # (left value or 0, right value or 0, length) of each run of -1
i = 0
while i < n:
if nums[i] != -1:
if i + 1 < n and nums[i + 1] != -1:
base = max(base, abs(nums[i] - nums[i + 1]))
i += 1
continue
j = i
while j + 1 < n and nums[j + 1] == -1:
j += 1
a = nums[i - 1] if i > 0 else 0
b = nums[j + 1] if j + 1 < n else 0
for v in (a, b):
if v:
lo = min(lo, v)
hi = max(hi, v)
runs.append((a, b, j - i + 1))
i = j + 1
if hi == 0:
return base
def ok(d: int) -> bool:
x, y = lo + d, max(hi - d, 1)
for a, b, length in runs:
good = False
for p in (x, y):
for q in (x, y):
if a and abs(a - p) > d:
continue
if b and abs(b - q) > d:
continue
if a and b:
if length == 1 and p != q:
continue
if abs(p - q) > d:
continue
good = True
if not good:
return False
return True
left, right = base, 10**9
while left < right:
mid = (left + right) // 2
if ok(mid):
right = mid
else:
left = mid + 1
return left