3919. Minimum Cost to Move Between Indices
You are given an integer array
numswherenumsis strictly increasing.For each index
x, letclosest(x)be the adjacent indexysuch thatabs(nums[x] - nums[y])is minimized. If both adjacent indices exist and give the same difference, choose the smaller index.From any index
x, you can move in two ways:
- To any index
ywith costabs(nums[x] - nums[y]), or- To
closest(x)with cost 1.You are also given a 2D integer array
queries, where eachqueries[i] = [l_i, r_i].For each query, calculate the minimum total cost to move from index
l_ito indexr_i.Return an integer array
ans, whereans[i]is the answer for thei^thquery.The absolute difference between two values
xandyis defined asabs(x - y).
1 | class Solution { |