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 maximum coins we can get from disappearing coins matrix in Python
When we have a 2D matrix where each cell represents coins, we need to find the maximum coins we can collect with specific constraints. When we pick coins from matrix[r, c], all coins in adjacent rows (r-1 and r+1) and adjacent columns (c-1 and c+1) disappear.
This problem can be solved using dynamic programming by treating it as two separate "House Robber" problems − first for each row, then for the resulting row sums.
Problem Understanding
Given the input matrix ?
| 2 | 8 | 7 | 6 |
| 10 | 10 | 4 | 2 |
| 5 | 9 | 2 | 3 |
We can pick cells with coins 8, 6, 9, and 3, giving us a total of 26 coins. Adjacent cells become unavailable when we make a selection.
Algorithm Approach
The solution uses a two-step approach ?
- Row-wise optimization: For each row, find the maximum coins we can collect without picking adjacent columns
- Column-wise optimization: Treat row sums as a 1D array and find maximum without picking adjacent rows
Helper Function - Maximum Non-Adjacent Sum
This function solves the classic "House Robber" problem for a 1D array ?
def getmax(arr):
prev_max, curr_max = 0, 0
res = 0
for num in arr:
temp = curr_max
curr_max = num + prev_max
prev_max = max(temp, prev_max)
res = max(res, curr_max)
return res
# Test the helper function
test_row = [2, 8, 7, 6]
print(f"Maximum from row {test_row}: {getmax(test_row)}")
Maximum from row [2, 8, 7, 6]: 14
Complete Solution
def getmax(arr):
prev_max, curr_max = 0, 0
res = 0
for num in arr:
temp = curr_max
curr_max = num + prev_max
prev_max = max(temp, prev_max)
res = max(res, curr_max)
return res
def solve(matrix):
if not matrix:
return 0
m = len(matrix)
row_sum = []
# Step 1: Find maximum coins for each row
for i in range(m):
row_sum.append(getmax(matrix[i]))
print(f"Row sums: {row_sum}")
# Step 2: Find maximum from row sums (no adjacent rows)
return getmax(row_sum)
# Test with the given matrix
matrix = [
[2, 8, 7, 6],
[10, 10, 4, 2],
[5, 9, 2, 3]
]
result = solve(matrix)
print(f"Maximum coins: {result}")
Row sums: [14, 12, 12] Maximum coins: 26
How It Works
The algorithm works in two phases ?
- Row Processing: For each row [2,8,7,6], [10,10,4,2], [5,9,2,3], we find the maximum non-adjacent sum: [14,12,12]
- Row Selection: From row sums [14,12,12], we select non-adjacent rows. We can pick first and third rows: 14 + 12 = 26
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(m × n) | We process each cell once |
| Space | O(m) | Store row sums array |
Conclusion
This problem combines two instances of the House Robber problem − first applied row-wise, then column-wise. The dynamic programming approach ensures we find the optimal solution by considering all valid combinations while respecting the adjacency constraints.
