# 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 :

```                        ```Solution in C++ :

int solve(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix.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[i] = matrix[i];
}

for (int i = 0; i < m; i++) {
if (i > 0) {
left[i] = max(left[i - 1], dp[i] + i);
} else {
left = dp;
}
int ri = m - 1 - i;
if (ri < m - 1) {
right[ri] = max(right[ri + 1], dp[ri] - ri);
} else {
right[ri] = dp[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 = dp[i];
}
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.length;
int NEGINF = Integer.MIN_VALUE;
int[][] dp = new int[N][M];
for (int j = 0; j < M; j++) {
dp[j] = matrix[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
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)```
```

## AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

## Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr