Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Move Between Indices

Try to solve the Minimum Cost to Move Between Indices problem.

Statement

You are given a strictly increasing zero indexed integer array nums of length nn, and an array queries where each query is given as queries[i] =[li,ri]= [l_i, r_i].

For any index i, its closest neighbor is defined as the adjacent index j (where ∣i−j∣=1|i - j| = 1) that minimizes ∣|nums[i] −- nums[j]∣|. If both adjacent indices have the same difference, either may be chosen as the closest neighbor.

When moving directly from index i to an adjacent index j:

  • The cost is 11 if j is the closest neighbor of i.

  • The cost is ∣|nums[i] −- nums[j]∣| otherwise.

For each query queries[i] =[li,ri]= [l_i, r_i], determine the minimum total cost to travel from index l_i to index r_i by moving only between adjacent indices.

Return an integer array ans where ans[i] is the minimum cost for the $i$th query.

Constraints:

  • 2≤2 \leq nums.length ≤105\leq 10^5

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9

  • nums is strictly increasing

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i] =[li,ri]= [l_i, r_i]

  • 0≤li,ri<0 \leq l_i, r_i < nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Move Between Indices

Try to solve the Minimum Cost to Move Between Indices problem.

Statement

You are given a strictly increasing zero indexed integer array nums of length nn, and an array queries where each query is given as queries[i] =[li,ri]= [l_i, r_i].

For any index i, its closest neighbor is defined as the adjacent index j (where ∣i−j∣=1|i - j| = 1) that minimizes ∣|nums[i] −- nums[j]∣|. If both adjacent indices have the same difference, either may be chosen as the closest neighbor.

When moving directly from index i to an adjacent index j:

  • The cost is 11 if j is the closest neighbor of i.

  • The cost is ∣|nums[i] −- nums[j]∣| otherwise.

For each query queries[i] =[li,ri]= [l_i, r_i], determine the minimum total cost to travel from index l_i to index r_i by moving only between adjacent indices.

Return an integer array ans where ans[i] is the minimum cost for the $i$th query.

Constraints:

  • 2≤2 \leq nums.length ≤105\leq 10^5

  • −109≤-10^9 \leq nums[i] ≤109\leq 10^9

  • nums is strictly increasing

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i] =[li,ri]= [l_i, r_i]

  • 0≤li,ri<0 \leq l_i, r_i < nums.length