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
|
||||
27
solutions/29/q2946/solution.go
Normal file
27
solutions/29/q2946/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue