- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a matrix M where M[r][c] represents the height of that cell. If we are currently at top left corner and want to go to the bottom right corner. We can move to adjacent cells (up, down, left, right) only if that the height of that adjacent cell is less than or equal to the current cell's height. We can increase the height of any number of cells before we move, so we have to find the minimum total height that needs to be increased so that we can go to the bottom right cell.

So, if the input is like

2 | 4 | 5 |

8 | 6 | 1 |

then the output will be 4, as we can take the following path [2, 4, 5, 1] and change the heights to this configuration −

5 | 5 | 5 |

8 | 6 | 1 |

To solve this, we will follow these steps −

INF := infinity

R, C := row number of matrix, column number of matrix

pq := make a priority queue using heap, and insert [0, R-1, C-1, M[-1, -1]] into it

dist := a map

dist[R-1, C-1, A[-1, -1]] := 0

while pq is not empty, do

delete one element from pq and store them into d, r, c, h

if dist[r, c, h] < d, then

go for the next iteration

if r and c are both 0, then

return d

for each pair (nr, nc) in [[r+1, c], [r, c+1], [r-1, c], [r, c-1]], do

if 0 <= nr < R and 0 <= nc < C, then

if d2 < dist[nr, nc, h2], then

dist[nr, nc, h2] := d2

insert [d2, nr, nc, h2] into pq

Let us see the following implementation to get better understanding −

import collections import heapq class Solution: def solve(self, A): INF = float('inf') R, C = len(A), len(A[0]) pq = [[0, R-1, C-1, A[-1][-1]]] dist = collections.defaultdict(lambda: INF) dist[R-1, C-1, A[-1][-1]] = 0 while pq: d, r, c, h = heapq.heappop(pq) if dist[r, c, h] < d: continue if r == c == 0: return d for nr, nc in [[r+1, c], [r, c+1], [r-1, c], [r, c-1]]: if 0 <= nr < R and 0 <= nc < C: h2 = max(A[nr][nc], h) d2 = d + max(h2 - A[nr][nc], 0) if d2 < dist[nr, nc, h2]: dist[nr, nc, h2] = d2 heapq.heappush(pq, [d2, nr, nc, h2]) ob = Solution() matrix = [ [2, 4, 5], [8, 6, 1] ] print(ob.solve(matrix))

[[2, 4, 5],[8, 6, 1]]

4

- Related Questions & Answers
- Minimum Initial Points to Reach Destination
- Find the minimum cost to reach destination using a train
- Program to find number of minimum steps to reach last index in Python
- Program to find minimum number of vertices to reach all nodes using Python
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find minimum jumps to reach home in Python
- Program to find minimum number of cells it will take to reach bottom right corner in Python
- Program to find number of combinations of coins to reach target in Python
- Count number of ways to reach destination in a Maze in C++
- Program to find minimum number of characters to be added to make it palindrome in Python
- Program to find number of given operations required to reach Target in Python
- C Program for Minimum number of jumps to reach the end
- Program to find out the minimum number of moves for a chess piece to reach every position in Python

Advertisements