add new solutions
This commit is contained in:
parent
1f0aa6d417
commit
efc7e67367
7 changed files with 215 additions and 2 deletions
30
solutions/29/q2906/solution.go
Normal file
30
solutions/29/q2906/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Package q2906 implements a solution for https://leetcode.com/problems/construct-product-matrix/
|
||||
package q2906
|
||||
|
||||
const MOD = 12345
|
||||
|
||||
func constructProductMatrix(grid [][]int) [][]int {
|
||||
w, h := len(grid[0]), len(grid)
|
||||
output := make([][]int, h)
|
||||
output[0] = make([]int, w*h)
|
||||
for i := h - 1; i >= 0; i-- {
|
||||
output[i] = output[0][i*w : (i+1)*w]
|
||||
}
|
||||
|
||||
suffix := 1
|
||||
for i := w*h - 1; i >= 0; i-- {
|
||||
y, x := i/w, i%w
|
||||
output[y][x] = suffix
|
||||
suffix = suffix * grid[y][x] % MOD
|
||||
}
|
||||
|
||||
prefix := 1
|
||||
for i := range w * h {
|
||||
y, x := i/w, i%w
|
||||
output[y][x] = output[y][x] * prefix % MOD
|
||||
prefix = prefix * grid[y][x] % MOD
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
var _ = constructProductMatrix
|
||||
Loading…
Add table
Add a link
Reference in a new issue