643. Jump Game VIII

You are provided a 0-indexed integer array nums of length n. You begin at index 0. You can leap from index i to a later index j (i < j) if one of the following criteria is met:

  • The Ascent Leap: nums[i] <= nums[j] and nums[k] < nums[i] for all indexes k in the range i < k < j, or
  • The Descent Leap: nums[i] > nums[j] and nums[k] >= nums[i] for all indexes k in the range i < k < j.

You are also given an integer array costs of length n where costs[i] denotes the cost of jumping to index i.

Return the minimum cost to jump to the index n - 1.

Example 1:

Input: nums = [3,2,4,4,1], costs = [3,7,6,4,2]

Output: 8

Explanation:

You start at index 0.

- Jump to index 2 with a cost of costs[2] = 6.

- Jump to index 4 with a cost of costs[4] = 2.

The total cost is 8. It can be proven that 8 is the minimum cost needed.

Two other possible paths are from index 0 -> 1 -> 4 and index 0 -> 2 -> 3 -> 4.

These have a total cost of 9 and 12, respectively.

Example 2:

Input: nums = [0,1,2], costs = [1,1,1]

Output: 2

Explanation:

Start at index 0.

- Jump to index 1 with a cost of costs[1] = 1.

- Jump to index 2 with a cost of costs[2] = 1.

The total cost is 2. Note that you cannot jump directly from index 0 to index 2 because nums[0] <= nums[1].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [5, 3, 6, 7, 2], costs = [4, 6, 5, 3, 2]

(Give answer with the output)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length == costs.length
  • 1 <= n <= 105
  • 0 <= nums[i], costs[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long minCost(vector<int>& nums, vector<int>& costs) {
// Your code goes here
}
};
Test Case

Input:

Costs
Nums