Max Multiplied Pairings Sequel - Google Top Interview Questions


Problem Statement :


You are given two lists of integers nums and multipliers.

For each i = 0 to i = multiplers.length - 1, inclusive, perform the following operation:

Remove one integer e from either the beginning or the end of nums
Add multipliers[i] * e to your running total
Return the maximum total possible after performing the operations.

Constraints

0 ≤ m ≤ n ≤ 1,000 where n is the length of nums and m is the length of multipliers

Example 1

Input

nums = [5, 2, -7]
multipliers = [2, 4, -1]

Output

25
Explanation

We can get 5 * 2 + 2 * 4 + -7 * -1 = 25



Solution :



title-img




                        Solution in C++ :

int solve(vector<int>& nums, vector<int>& m) {
    if (m.empty()) return 0;
    if (m.size() == 1) return max(m[0] * nums.front(), m[0] * nums.back());
    int M = nums.size(), N = m.size(), ans = INT_MIN;
    vector<vector<int>> dp(N + 1, vector<int>(N + 1, INT_MIN));
    dp[1][0] = m[0] * nums.front();
    dp[0][1] = m[0] * nums.back();
    for (int i = 2; i <= N; i++) {
        for (int j = 0; j <= i; j++) {
            int a = j == i ? INT_MIN : (dp[i - j - 1][j] + nums[i - j - 1] * m[i - 1]);
            int b = j == 0 ? INT_MIN : (dp[i - j][j - 1] + nums[M - j] * m[i - 1]);
            dp[i - j][j] = max(a, b);
            if (i == N) ans = max(ans, dp[i - j][j]);
        }
    }
    return ans;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[] nums, int[] multipliers) {
        if (nums == null || nums.length == 0)
            return 0;
        return getMax(
            nums, multipliers, 0, nums.length - 1, 0, multipliers.length - 1, new HashMap());
    }

    private int getMax(int[] nums, int[] mul, int low, int high, int idx, int high2,
        Map<String, Integer> memo_map) {
        if (idx == high2)
            return Math.max(nums[low] * mul[idx], nums[high] * mul[idx]);

        String str = new StringBuilder().append(low).append(high).append(idx).toString();
        if (memo_map.containsKey(str))
            return memo_map.get(str);

        int val1 =
            nums[low] * mul[idx] + getMax(nums, mul, low + 1, high, idx + 1, high2, memo_map);
        int val2 =
            nums[high] * mul[idx] + getMax(nums, mul, low, high - 1, idx + 1, high2, memo_map);

        memo_map.put(str, Math.max(val1, val2));
        return memo_map.get(str);
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, nums, mult):
        n, m = len(nums), len(mult)

        @functools.cache
        def go(i, j):
            if j == m:
                return 0
            return max(
                nums[i] * mult[j] + go(i + 1, j + 1), nums[n - j + i - 1] * mult[j] + go(i, j + 1)
            )

        return go(0, 0)
                    


View More Similar Problems

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →