From fa59e60f3549f5f83548b25c69df32ebfdb22533 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 20 Jul 2024 22:18:52 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1605.=20Find=20Valid=20Matrix?= =?UTF-8?q?=20Given=20Row=20and=20Column=20Sums=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...-valid-matrix-given-row-and-column-sums.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 go/find-valid-matrix-given-row-and-column-sums.go diff --git a/go/find-valid-matrix-given-row-and-column-sums.go b/go/find-valid-matrix-given-row-and-column-sums.go new file mode 100644 index 0000000..9cdd366 --- /dev/null +++ b/go/find-valid-matrix-given-row-and-column-sums.go @@ -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 +}