add new solutions

This commit is contained in:
Yiyang Kang 2026-03-27 11:28:00 +09:00
parent 1f0aa6d417
commit efc7e67367
Signed by: kkyy
SSH key fingerprint: SHA256:lJSbAzC3MvrSORdvIVK6h/3g+rVKJNzM7zq0MgA9WKY
7 changed files with 215 additions and 2 deletions

View 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

View file

@ -0,0 +1,27 @@
// Package q2946 implements a solution for https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts/
package q2946
func areSimilar(mat [][]int, k int) bool {
w := len(mat[0])
k %= w
if k == 0 {
return true
}
for r := range mat {
for c := range w {
var shifted int
if r%2 == 0 {
shifted = (c + k) % w
} else {
shifted = (c + w - k) % w
}
if mat[r][shifted] != mat[r][c] {
return false
}
}
}
return true
}
var _ = areSimilar