'Creating matrix with getting maximum of each row and column
I want to make the minimum matrix given the max of each row and column.
Example:
Given:
row_max = [50, 20]
col_max = [50, 20, 3]
Result:
array = [[50, 0, 0],
[0, 20, 3]]
Solution 1:[1]
Let maxColIndex be column's index which contains maximum value within col_max:
col_max = [50, 20, 3] # maxColIndex = 0
Let maxRowIndex be row's index which contains maximum value within row_max:
row_max = [10, 50, 20, 5] # maxRowIndex = 1
So we have
| 50 20 3
-------------
10 | ? ? ?
50 | ? ? ? <- maxRowIndex
20 | ? ? ?
5 | ? ? ?
^
maxColIndex
Now our goal is to put as many 0 as possible. Please note, that if there are same values in both max and min rows collections (e.g. 50, 20) we can put just one value:
| 50 20 3
-------------
10 | ? ? ?
50 | 50 ? ? <- maxRowIndex
20 | ? 20 ?
5 | ? ? ?
^
maxColIndex
Now put all the rest row_max values into the maxColIndex column:
| 50 20 3
-------------
10 | 10 ? ?
50 | 50 ? ? <- maxRowIndex
20 | ? 20 ?
5 | 5 ? ?
^
maxColIndex
Finally, put all the rest col_max values into the maxRowIndex row:
| 50 20 3
-------------
10 | 10 ? ?
50 | 50 ? 3 <- maxRowIndex
20 | ? 20 ?
5 | 5 ? ?
^
maxColIndex
All indexes are set, time to fill the unset items of the matrix
| 50 20 3
-------------
10 | 10 0 0
50 | 50 0 3 <- maxRowIndex
20 | 0 20 0
5 | 5 0 0
^
maxColIndex
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitry Bychenko |
