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[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
Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →