Hop Cost - Google Top Interview Questions


Problem Statement :


You are given two lists of integers nums0 and nums1 of the same length as well as integers dist and cost.

You must start off at index 0 at either nums0 or nums1 and want to end up at the last index of either list. 

In each round, you can choose to switch to the other list for cost of cost. 

And then you can jump forward at most dist distance away where the cost of landing at an index is the value at that index.

Return the minimum total cost possible to finish the task.

Constraints

0 ≤ n ≤ 1,000 where n is the length of nums0 and nums1.

dist ≤ 100

Example 1

Input

nums0 = [1, 2, 9, 9, 5]

nums1 = [9, 9, 3, 4, 100]

dist = 2


cost = 3

Output

15

Explanation

We can start off at 1, switch to the second list to 3, and switch back to first list to 5.


The total cost is 1 + 3 + 5 plus the two jumps across lists 3 + 3.



Solution :



title-img




                        Solution in C++ :

const int NTREE = 1024;

void update(vector<int>& tree, int pos, int val) {
    pos += NTREE;
    tree[pos] = val;
    pos /= 2;
    while (pos) {
        tree[pos] = min(tree[2 * pos], tree[2 * pos + 1]);
        pos /= 2;
    }
}

int query(vector<int>& tree, int l, int r) {
    int result = INT_MAX;
    l += NTREE;
    r += NTREE;

    while (l <= r) {
        if (l % 2 == 1) result = min(result, tree[l++]);
        if (r % 2 == 0) result = min(result, tree[r--]);

        l /= 2;
        r /= 2;
    }

    return result;
}

int solve(vector<int>& nums0, vector<int>& nums1, int dist, int cost) {
    int N = nums0.size();
    vector<int> tree0, tree1;
    tree0.resize(2 * NTREE);
    tree1.resize(2 * NTREE);

    for (int i = 0; i < 2 * NTREE; i++) tree0[i] = tree1[i] = INT_MAX;

    update(tree0, 0, nums0[0]);
    update(tree1, 0, nums1[0]);

    for (int i = 1; i < N; i++) {
        int min0 = query(tree0, i - dist, i - 1);
        int min1 = query(tree1, i - dist, i - 1);

        int dp0 = min(min0, min1 + cost) + nums0[i];
        int dp1 = min(min1, min0 + cost) + nums1[i];

        update(tree0, i, dp0);
        update(tree1, i, dp1);
    }

    return min(query(tree0, N - 1, N - 1), query(tree1, N - 1, N - 1));
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[] nums0, int[] nums1, int dist, int cost) {
        int n = nums0.length, a0[] = nums0.clone(), a1[] = nums1.clone();
        for (int i = 1; i < n; i++) {
            a0[i] += a0[i - 1];
            a1[i] += a1[i - 1];
            for (int j = i - 1; j >= 0 && j >= i - dist; j--) {
                a1[i] = Math.min(a1[i], Math.min(a1[j], a0[j] + cost) + nums1[i]);
                a0[i] = Math.min(a0[i], Math.min(a0[j], a1[j] + cost) + nums0[i]);
            }
        }
        return Math.min(a0[n - 1], a1[n - 1]);
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, nums0, nums1, dist, cost):
        n = len(nums0)
        if n == 0:
            return 0
        dp0, dp1 = [0] * n, [0] * n
        monoq0, monoq1 = deque(), deque()
        dp0[0] = nums0[0]
        dp1[0] = nums1[0]
        monoq0.append(nums0[0])
        monoq1.append(nums1[0])
        for i in range(1, n):
            # calculate best costs at current index
            dp0[i] = nums0[i] + min(monoq0[0], monoq1[0] + cost)
            dp1[i] = nums1[i] + min(monoq0[0] + cost, monoq1[0])

            # update monoqueue for dp0
            while len(monoq0) > 0 and monoq0[-1] > dp0[i]:
                monoq0.pop()
            monoq0.append(dp0[i])
            if i >= dist and monoq0[0] == dp0[i - dist]:
                monoq0.popleft()

            # update monoqueue for dp1
            while len(monoq1) > 0 and monoq1[-1] > dp1[i]:
                monoq1.pop()
            monoq1.append(dp1[i])
            if i >= dist and monoq1[0] == dp1[i - dist]:
                monoq1.popleft()

        return min(dp0[n - 1], dp1[n - 1])
                    


View More Similar Problems

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →