22. Make Matrix Beautiful
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
The concept involves determining the highest sum of every row and column, and then executing actions on the rows and columns with sums lower than the maximum calculated sum. To do so I follow these steps.
Calculate the sum of each row and column while traversing the matrix.
Track the maximum sum of rows and columns.
Calculate the total sum of all elements in the matrix.
Return the maximum of (maxRow * n) and (maxCol * n) minus the total sum.
Let's illustrate this approach with an example: Suppose we have the following 3x3 matrix:
Calculate the sum of each row and column:
Row sums: [6, 15, 24]
Column sums: [12, 15, 18]
Find the maximum sum among rows and columns, which is 24
(from row sums) so we focus on this sum and try to make all row and column sums equal to 24.
Calculate the total sum of all elements, which is 45
.
The minimum number of operations => (Maximum sum) x (n) - (Total sum) = (24 x 3 ) - 45
= 72 - 45
= 27
operations are required to make the matrix beautiful.
Time Complexity: O(n^2)
- We iterate through the matrix once to calculate row and column sums.
Auxiliary Space Complexity: O(1)
- We use a few variables to store sums and results, and not any additional data structures.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.