Databricks | OA 2023

Anonymous User
1945
Oct 25, 2023

You are given a matrix A with N rows and M columns.
You can start from any row on the 1st column, and your objective is to reach any row on the last column with minimum cost.
From cell (ith row, jth col), you can jump to any row in the (j+1)th col.

Let P1, P2, ...PM be the m elements which are traversed while going from 1st col to Mth col. Then the cost of this path is the absolute difference between max(P1, P2, .... PM) and min(P1, P2, ...PM).

Find the minimum cost possible to go from 1st column to Mth column.

Constraints:
1<= M,N <= 500
1 <= element of A <= 10^9

Example:
Input:
M = 3, N = 3
matrix A :
1 4 5
2 9 3
3 1 1

Output:
0
Explanation:
we can pick 1 in 1st col, 1 in 2nd col and 1 in 3rd col. Cost = 1 - 1 = 0

Can you please help solve this problem?

Comments (10)