- 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

# Program to find minimum number of heights to be increased to reach destination in Python

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 −

## Example

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))

## Input

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

## Output

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 number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to find minimum jumps to reach home in Python
- C++ program to find out the minimum amount of time needed to reach from source to destination station by train
- 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++
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find minimum number of characters to be added to make it palindrome in Python