Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of heights to be increased to reach destination in Python
Sometimes we need to find the minimum height increase required to reach from the top-left corner to the bottom-right corner of a matrix. We can only move to adjacent cells if their height is less than or equal to our current cell's height, but we can increase any cell's height before moving.
Problem Understanding
Given a matrix M where M[r][c] represents the height of that cell, we need to find the minimum total height increase to create a path from (0,0) to (R-1,C-1). Movement is allowed to adjacent cells (up, down, left, right) only if the destination cell's height ? current cell's height.
Example
Consider this matrix ?
| 2 | 4 | 5 |
| 8 | 6 | 1 |
The output will be 4. We can take path [2, 4, 5, 1] and increase heights to ?
| 5 | 5 | 5 |
| 8 | 6 | 1 |
Algorithm
This problem uses Dijkstra's algorithm with a modified state: (row, col, height). We start from the destination and work backwards to find the minimum cost path ?
- Use a priority queue to explore paths in order of increasing cost
- State: (distance, row, col, current_height)
- For each neighbor, calculate the required height and cost
- Track visited states to avoid recomputation
Implementation
import collections
import heapq
class Solution:
def solve(self, A):
INF = float('inf')
R, C = len(A), len(A[0])
# Start from bottom-right corner
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)
# Skip if we found a better path already
if dist[r, c, h] < d:
continue
# Reached top-left corner
if r == c == 0:
return d
# Explore all 4 directions
for nr, nc in [[r+1, c], [r, c+1], [r-1, c], [r, c-1]]:
if 0 <= nr < R and 0 <= nc < C:
# Height needed at (nr, nc) to move to (r, c)
h2 = max(A[nr][nc], h)
# Cost = current distance + height increase needed
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])
# Test the solution
ob = Solution()
matrix = [
[2, 4, 5],
[8, 6, 1]
]
print(ob.solve(matrix))
4
How It Works
The algorithm works backwards from destination to source ?
- State representation: (distance, row, col, height) where height is the minimum height needed at this cell
- Cost calculation: When moving from (nr,nc) to (r,c), we need height h2 = max(original_height, required_height)
- Height increase: Cost increases by max(h2 - original_height, 0)
- Dijkstra's guarantee: First time we reach (0,0), we have the minimum cost
Time Complexity
The time complexity is O(R × C × H × log(R × C × H)) where H is the maximum possible height, and space complexity is O(R × C × H) for the distance map.
Conclusion
This solution uses Dijkstra's algorithm with a 3D state space to find the minimum height increase needed. The key insight is working backwards from destination and tracking the required height at each cell.
