go: add «1605. Find Valid Matrix Given Row and Column Sums»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-20 22:18:52 +02:00
parent bf30ec7ec7
commit fa59e60f35
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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
}