Maximum Dropping Path Sum With Column Distance Cost - Google Top Interview Questions


Problem Statement :


You are given a two-dimensional list of integers matrix. 

You want to pick a number from each row. For each 0 ≤ r < n - 1 the cost for picking matrix[r][j] and matrix[r + 1][k] is abs(k - j).

Return the maximum sum possible of the numbers chosen, minus costs.

Constraints

1 ≤ n * m ≤ 200,000 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [

    [3, 2, 1, 6],

    [4, 1, 2, 0],

    [1, 5, 2, -2]

]

Output

11

Explanation

Return 11 by picking 3, 4 and 5.



Example 2

Input
matrix = [

    [7, 6, 5, 6],

    [6, 4, 5, 8]

]

Output

14

Explanation

We can take the last 6 in the first row and 8 in the last row.


Example 3

Input

matrix = [

    [2, 1, 3]

]

Output

3
Explanation

We can take 3.



Solution :



title-img




                        Solution in C++ :

int solve(vector<vector<int>>& matrix) {
    int n = matrix.size();
    int m = matrix[0].size();
    vector<vector<int>> dp(n, vector<int>(m, INT_MIN));
    vector<int> left(m, INT_MIN), right(m, INT_MIN);
    for (int i = 0; i < m; i++) {
        dp[0][i] = matrix[0][i];
    }

    for (int i = 0; i < m; i++) {
        if (i > 0) {
            left[i] = max(left[i - 1], dp[0][i] + i);
        } else {
            left[0] = dp[0][0];
        }
        int ri = m - 1 - i;
        if (ri < m - 1) {
            right[ri] = max(right[ri + 1], dp[0][ri] - ri);
        } else {
            right[ri] = dp[0][ri] - ri;
        }
    }

    for (int i = 1; i < n; i++) {
        for (int j = 0; j < m; j++) {
            dp[i][j] = max({dp[i][j], left[j] + matrix[i][j] - j, right[j] + matrix[i][j] + j});
        }
        for (int j = 0; j < m; j++) {
            left[j] = INT_MIN;
            if (j > 0) {
                left[j] = max(left[j - 1], dp[i][j] + j);
            } else {
                left[0] = dp[i][0];
            }
            int ri = m - 1 - j;
            right[ri] = INT_MIN;
            if (ri < m - 1) {
                right[ri] = max(right[ri + 1], dp[i][ri] - ri);
            } else {
                right[ri] = dp[i][ri] - ri;
            }
        }
    }
    int ans = *max_element(dp[n - 1].begin(), dp[n - 1].end());
    return ans;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[][] matrix) {
        int N = matrix.length;
        int M = matrix[0].length;
        int NEGINF = Integer.MIN_VALUE;
        int[][] dp = new int[N][M];
        for (int j = 0; j < M; j++) {
            dp[0][j] = matrix[0][j];
        }

        for (int i = 1; i < N; i++) {
            int[] right = new int[M + 1];
            right[M] = NEGINF;
            for (int j = M - 1; j >= 0; j--) right[j] = Math.max(right[j + 1], dp[i - 1][j] - j);
            int left = NEGINF;
            for (int j = 0; j < M; j++) {
                left = Math.max(left, dp[i - 1][j] + j);
                dp[i][j] = matrix[i][j] + Math.max(left - j, right[j] + j);
            }
        }
        int ans = NEGINF;
        for (int j = 0; j < M; j++) {
            ans = Math.max(ans, dp[N - 1][j]);
        }
        return ans;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, matrix):
        dp = matrix[0]
        for i in range(1, len(matrix)):
            ndp = [float(-inf)] * len(matrix[i])
            lhs = float(-inf)
            for j in range(len(matrix[i])):
                lhs = max(lhs, dp[j])
                ndp[j] = max(ndp[j], lhs + matrix[i][j])
                lhs -= 1
            rhs = float(-inf)
            for j in range(len(matrix[i]) - 1, -1, -1):
                rhs = max(rhs, dp[j])
                ndp[j] = max(ndp[j], rhs + matrix[i][j])
                rhs -= 1
            dp = ndp
        return max(dp)
                    


View More Similar Problems

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →