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

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →