go: add «1605. Find Valid Matrix Given Row and Column Sums»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
bf30ec7ec7
commit
fa59e60f35
1 changed files with 26 additions and 0 deletions
26
go/find-valid-matrix-given-row-and-column-sums.go
Normal file
26
go/find-valid-matrix-given-row-and-column-sums.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
func restoreMatrix(rowSum []int, colSum []int) [][]int {
|
||||
rows, cols := len(rowSum), len(colSum)
|
||||
|
||||
matrix := make([][]int, rows)
|
||||
for y := range rows {
|
||||
matrix[y] = make([]int, cols)
|
||||
}
|
||||
|
||||
y, x := 0, 0
|
||||
for y < rows && x < cols {
|
||||
matrix[y][x] = min(rowSum[y], colSum[x])
|
||||
|
||||
rowSum[y] -= matrix[y][x]
|
||||
colSum[x] -= matrix[y][x]
|
||||
|
||||
if rowSum[y] == 0 {
|
||||
y++
|
||||
} else {
|
||||
x++
|
||||
}
|
||||
}
|
||||
|
||||
return matrix
|
||||
}
|
Loading…
Reference in a new issue