Minimum Cost to Move Between Indices Problem
Description
LeetCode Problem 3919.
You are given an integer array nums where nums is strictly increasing.
For each index x, let closest(x) be the adjacent index y such that abs(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 each queries[i] = [l_i, r_i].
For each query, calculate the minimum total cost to move from index l_i to index r_i.
Return an integer array ans, where ans[i] is the answer for the i^th query.
The absolute difference between two values x and y is defined as abs(x - y).
Example 1:
Input: nums = [-5,-2,3], queries = [[0,2],[2,0],[1,2]]
Output: [6,2,5]
Explanation:
- The closest indices are [1, 0, 1] respectively.
- For [0, 2], the path 0 → 1 → 2 uses a closest move from index 0 to 1 with cost 1 and a move from index 1 to 2 with cost |-2 - 3| = 5, giving total 1 + 5 = 6.
- For [2, 0], the path 2 → 1 → 0 uses two closest moves from index 2 to 1 and from index 1 to 0, each with cost 1, giving total 2.
- For [1, 2], the direct move from index 1 to index 2 has cost |-2 - 3| = 5, which is optimal.
Thus, ans = [6, 2, 5].
Example 2:
Input: nums = [0,2,3,9], queries = [[3,0],[1,2],[2,0]]
Output: [4,1,3]
Explanation:
- The closest indices are [1, 2, 1, 2] respectively.
- For [3, 0], the path 3 → 2 → 1 → 0 uses closest moves from index 3 to 2 and from 2 to 1, each with cost 1, and a move from 1 to 0 with cost |2 - 0| = 2, giving total 1 + 1 + 2 = 4.
- For [1, 2], the closest move from index 1 to 2 has cost 1.
- For [2, 0], the path 2 → 1 → 0 uses a closest move from index 2 to 1 with cost 1 and a move from 1 to 0 with cost |2 - 0| = 2, giving total 1 + 2 = 3.
Thus, ans = [4, 1, 3].
Constraints:
2 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9numsis strictly increasing1 <= queries.length <= 10^5queries[i] = [l_i, r_i]0 <= l_i, r_i < nums.length
Solution
Approach: Prefix Sums in Each Direction
Because nums is strictly increasing, a jump to a far index costs the same as walking over the gaps in between, so it is enough to walk between neighboring indices. Each step between neighbors costs either 1 (when it is a "closest" move) or the gap between the two values.
Moving right from i - 1 to i costs 1 if closest(i - 1) = i and the gap nums[i] - nums[i - 1] otherwise. Moving left from i to i - 1 costs 1 if closest(i) = i - 1 and the gap otherwise. The closest neighbor is decided by comparing the two adjacent gaps, with a tie going to the smaller index.
Compute the cost of every rightward step and of every leftward step, and build prefix sums s1 (rightward) and s2 (leftward). A query [l, r] with l < r is answered by s1[r] - s1[l], otherwise (l >= r, where l == r gives 0) by s2[l] - s2[r].
Complexity: time O(n + q) and space O(n), where n is the length of nums and q the number of queries.
class Solution:
def minCost(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n = len(nums)
s1 = [0] * n
s2 = [0] * n
for i in range(1, n):
c1 = (
nums[i] - nums[i - 1]
if i > 1 and nums[i - 1] - nums[i - 2] <= nums[i] - nums[i - 1]
else 1
)
c2 = (
nums[i] - nums[i - 1]
if i < n - 1 and nums[i] - nums[i - 1] > nums[i + 1] - nums[i]
else 1
)
s1[i] = s1[i - 1] + c1
s2[i] = s2[i - 1] + c2
m = len(queries)
ans = [0] * m
for i, (l, r) in enumerate(queries):
ans[i] = s1[r] - s1[l] if l < r else s2[l] - s2[r]
return ans