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
Selected Reading
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only in Python
Given two matrices of size n x m named mat1 and mat2, we need to check if both matrices can be made strictly increasing by swapping corresponding elements at position (i, j) between the matrices. A matrix is strictly increasing if each element is smaller than the element to its right and below it.
Problem Understanding
Consider these input matrices ?
Algorithm
The solution follows a greedy approach ?
- Ensure optimal arrangement: For each position (i,j), if mat1[i][j] > mat2[i][j], swap them to minimize mat1 values
- Check row-wise ordering: Verify each row is strictly increasing in both matrices
- Check column-wise ordering: Verify each column is strictly increasing in both matrices
Implementation
def solve(mat1, mat2):
row = len(mat1)
col = len(mat1[0])
# Step 1: Ensure mat1[i][j] <= mat2[i][j] for optimal arrangement
for i in range(row):
for j in range(col):
if mat1[i][j] > mat2[i][j]:
mat1[i][j], mat2[i][j] = mat2[i][j], mat1[i][j]
# Step 2: Check row-wise strictly increasing order
for i in range(row):
for j in range(col - 1):
if mat1[i][j] >= mat1[i][j + 1] or mat2[i][j] >= mat2[i][j + 1]:
return False
# Step 3: Check column-wise strictly increasing order
for i in range(row - 1):
for j in range(col):
if mat1[i][j] >= mat1[i + 1][j] or mat2[i][j] >= mat2[i + 1][j]:
return False
return True
# Test the function
mat1 = [[7, 15],
[16, 10]]
mat2 = [[14, 9],
[8, 17]]
print("Input matrices:")
print(f"mat1: {mat1}")
print(f"mat2: {mat2}")
print(f"Result: {solve(mat1, mat2)}")
Input matrices: mat1: [[7, 15], [16, 10]] mat2: [[14, 9], [8, 17]] Result: True
How It Works
Let's trace through the algorithm step by step ?
def solve_with_trace(mat1, mat2):
# Create copies to avoid modifying original matrices
mat1 = [row[:] for row in mat1]
mat2 = [row[:] for row in mat2]
row = len(mat1)
col = len(mat1[0])
print("Original matrices:")
print(f"mat1: {mat1}")
print(f"mat2: {mat2}")
# Step 1: Optimal swapping
swaps_made = []
for i in range(row):
for j in range(col):
if mat1[i][j] > mat2[i][j]:
mat1[i][j], mat2[i][j] = mat2[i][j], mat1[i][j]
swaps_made.append(f"Position ({i},{j})")
print(f"\nSwaps made at: {swaps_made}")
print(f"After swapping:")
print(f"mat1: {mat1}")
print(f"mat2: {mat2}")
# Check if both matrices are strictly increasing
return True # Simplified for demonstration
# Test
mat1 = [[7, 15], [16, 10]]
mat2 = [[14, 9], [8, 17]]
solve_with_trace(mat1, mat2)
Original matrices: mat1: [[7, 15], [16, 10]] mat2: [[14, 9], [8, 17]] Swaps made at: ['Position (0,0)', 'Position (1,1)'] After swapping: mat1: [[7, 9], [8, 10]] mat2: [[14, 15], [16, 17]]
Key Points
- Greedy Strategy: Always place smaller values in mat1 and larger values in mat2
- Two-phase Validation: Check both row-wise and column-wise ordering
- Time Complexity: O(n × m) where n is rows and m is columns
- Space Complexity: O(1) as we modify matrices in-place
Additional Example
# Example where it's not possible
mat1 = [[5, 4],
[3, 2]]
mat2 = [[1, 6],
[7, 8]]
print("Testing impossible case:")
print(f"mat1: {mat1}")
print(f"mat2: {mat2}")
print(f"Result: {solve(mat1, mat2)}")
Testing impossible case: mat1: [[5, 4], [3, 2]] mat2: [[1, 6], [7, 8]] Result: False
Conclusion
This greedy algorithm efficiently determines if two matrices can be made strictly increasing by swapping corresponding elements. The key insight is to always arrange smaller values in the first matrix for optimal results.
Advertisements
