add new solutions
This commit is contained in:
parent
1f0aa6d417
commit
efc7e67367
7 changed files with 215 additions and 2 deletions
4
go.mod
4
go.mod
|
|
@ -10,6 +10,6 @@ require (
|
|||
require (
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
golang.org/x/text v0.34.0 // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
golang.org/x/text v0.35.0 // indirect
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -9,5 +9,9 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
|
|||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
|
||||
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
|
||||
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
|
||||
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
|
||||
|
|
|
|||
35
solutions/15/q1594/solution.go
Normal file
35
solutions/15/q1594/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Package q1594 implements a solution for https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix/
|
||||
package q1594
|
||||
|
||||
const MOD = 1e9 + 7
|
||||
|
||||
func maxProductPath(grid [][]int) int {
|
||||
buf := make([][2]int, len(grid[0]))
|
||||
|
||||
buf[0] = [2]int{grid[0][0], grid[0][0]}
|
||||
for c := 1; c < len(grid[0]); c++ {
|
||||
num := grid[0][c]
|
||||
n1, n2 := num*buf[c-1][0], num*buf[c-1][1]
|
||||
buf[c] = [2]int{max(n1, n2), min(n1, n2)}
|
||||
}
|
||||
|
||||
for r := 1; r < len(grid); r++ {
|
||||
num := grid[r][0]
|
||||
n1, n2 := num*buf[0][0], num*buf[0][1]
|
||||
buf[0] = [2]int{max(n1, n2), min(n1, n2)}
|
||||
|
||||
for c := 1; c < len(grid[0]); c++ {
|
||||
num := grid[r][c]
|
||||
n1, n2 := num*buf[c][0], num*buf[c][1]
|
||||
n3, n4 := num*buf[c-1][0], num*buf[c-1][1]
|
||||
buf[c] = [2]int{
|
||||
max(n1, n2, n3, n4),
|
||||
min(n1, n2, n3, n4),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return max(-1, buf[len(buf)-1][0]%MOD)
|
||||
}
|
||||
|
||||
var _ = maxProductPath
|
||||
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
|
||||
39
solutions/35/q3546/solution.go
Normal file
39
solutions/35/q3546/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// Package q3546 implements a solution for https://leetcode.com/problems/equal-sum-grid-partition-i/
|
||||
package q3546
|
||||
|
||||
func chk(sums []int) bool {
|
||||
sumAll := 0
|
||||
for _, a := range sums {
|
||||
sumAll += a
|
||||
}
|
||||
if sumAll%2 != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
target := sumAll / 2
|
||||
sum := 0
|
||||
for _, x := range sums {
|
||||
sum += x
|
||||
if sum == target {
|
||||
return true
|
||||
} else if sum > target {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func canPartitionGrid(grid [][]int) bool {
|
||||
hSums := make([]int, len(grid[0]))
|
||||
vSums := make([]int, len(grid))
|
||||
|
||||
for r := range grid {
|
||||
for c := range grid[0] {
|
||||
hSums[c] += grid[r][c]
|
||||
vSums[r] += grid[r][c]
|
||||
}
|
||||
}
|
||||
return chk(hSums) || chk(vSums)
|
||||
}
|
||||
|
||||
var _ = canPartitionGrid
|
||||
78
solutions/35/q3548/solution.go
Normal file
78
solutions/35/q3548/solution.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// Package q3548 implements a solution for https://leetcode.com/problems/equal-sum-grid-partition-ii/
|
||||
package q3548
|
||||
|
||||
func findNumber(
|
||||
num int,
|
||||
r1, c1, r2, c2 int,
|
||||
byVal map[int][][2]int32,
|
||||
grid [][]int,
|
||||
) bool {
|
||||
w, h := c2-c1+1, r2-r1+1
|
||||
if w == 1 || h == 1 {
|
||||
return grid[r1][c1] == num || grid[r2][c2] == num
|
||||
}
|
||||
for _, coord := range byVal[num] {
|
||||
r, c := int(coord[0]), int(coord[1])
|
||||
if r >= r1 && r <= r2 && c >= c1 && c <= c2 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func canPartitionGrid(grid [][]int) bool {
|
||||
w, h := len(grid[0]), len(grid)
|
||||
hSums, vSums := make([]int, w), make([]int, h)
|
||||
sumAll := 0
|
||||
byVal := make(map[int][][2]int32, w*h)
|
||||
|
||||
for r := range h {
|
||||
for c := range w {
|
||||
val := grid[r][c]
|
||||
hSums[c] += val
|
||||
vSums[r] += val
|
||||
sumAll += val
|
||||
byVal[val] = append(byVal[val], [2]int32{int32(r), int32(c)})
|
||||
}
|
||||
}
|
||||
|
||||
// Check vertical cuts
|
||||
sum := 0
|
||||
for i := range w - 1 {
|
||||
sum += hSums[i]
|
||||
rem := sumAll - sum
|
||||
if sum > rem {
|
||||
if findNumber(sum-rem, 0, 0, h-1, i, byVal, grid) {
|
||||
return true
|
||||
}
|
||||
} else if sum < rem {
|
||||
if findNumber(rem-sum, 0, i+1, h-1, w-1, byVal, grid) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Check horizontal cuts
|
||||
sum = 0
|
||||
for i := range h - 1 {
|
||||
sum += vSums[i]
|
||||
rem := sumAll - sum
|
||||
if sum > rem {
|
||||
if findNumber(sum-rem, 0, 0, i, w-1, byVal, grid) {
|
||||
return true
|
||||
}
|
||||
} else if sum < rem {
|
||||
if findNumber(rem-sum, i+1, 0, h-1, w-1, byVal, grid) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
var _ = canPartitionGrid
|
||||
Loading…
Add table
Add a link
Reference in a new issue