add new solutions
This commit is contained in:
parent
f297e11859
commit
4720cbefc4
8 changed files with 290 additions and 0 deletions
33
solutions/17/q1727/solution.go
Normal file
33
solutions/17/q1727/solution.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// Package q1727 implements a solution for https://leetcode.com/problems/largest-submatrix-with-rearrangements/
|
||||
package q1727
|
||||
|
||||
import "slices"
|
||||
|
||||
func largestSubmatrix(matrix [][]int) int {
|
||||
maxArea := 0
|
||||
|
||||
// Count from the bottom the length of all contiguous segments of every column.
|
||||
for col := range matrix[0] {
|
||||
ones := 0
|
||||
for row := len(matrix) - 1; row >= 0; row-- {
|
||||
if matrix[row][col] == 0 {
|
||||
ones = 0
|
||||
} else {
|
||||
ones += 1
|
||||
matrix[row][col] = ones
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, row := range matrix {
|
||||
slices.SortFunc(row, func(a, b int) int { return b - a })
|
||||
minLen := row[0]
|
||||
for col := range row {
|
||||
minLen = min(minLen, row[col])
|
||||
maxArea = max(maxArea, minLen*(col+1))
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
var _ = largestSubmatrix
|
||||
75
solutions/18/q1878/solution.go
Normal file
75
solutions/18/q1878/solution.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// Package q1878 implements a solution for https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid/
|
||||
package q1878
|
||||
|
||||
import "slices"
|
||||
|
||||
func getBiggestThree(grid [][]int) []int {
|
||||
w, h := len(grid[0]), len(grid)
|
||||
sums := make([][][2]int, h)
|
||||
sums[0] = make([][2]int, w*h)
|
||||
for s := h - 1; s >= 0; s-- {
|
||||
sums[s] = sums[0][s*w : (s+1)*w]
|
||||
}
|
||||
|
||||
for r := range h {
|
||||
for c := range w {
|
||||
pR, pC := r-1, c-1
|
||||
if pR >= 0 && pC >= 0 {
|
||||
sums[r][c][0] = sums[pR][pC][0] + grid[r][c]
|
||||
} else {
|
||||
sums[r][c][0] = grid[r][c]
|
||||
}
|
||||
|
||||
pR, pC = r-1, c+1
|
||||
if pR >= 0 && pC < w {
|
||||
sums[r][c][1] = sums[pR][pC][1] + grid[r][c]
|
||||
} else {
|
||||
sums[r][c][1] = grid[r][c]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
biggest := []int{0, 0, 0, 0}
|
||||
var addResult = func(val int) {
|
||||
for i := 1; i <= 3; i++ {
|
||||
if biggest[i] == val {
|
||||
return
|
||||
}
|
||||
}
|
||||
biggest[0] = val
|
||||
slices.Sort(biggest)
|
||||
}
|
||||
|
||||
var isValid = func(coord [2]int) bool {
|
||||
return coord[0] >= 0 && coord[1] >= 0 && coord[0] < h && coord[1] < w
|
||||
}
|
||||
|
||||
for r := range h {
|
||||
for c := range w {
|
||||
addResult(grid[r][c])
|
||||
|
||||
for edge := 2; ; edge++ {
|
||||
left := [2]int{r + edge - 1, c - edge + 1}
|
||||
right := [2]int{r + edge - 1, c + edge - 1}
|
||||
bottom := [2]int{r + (edge-1)*2, c}
|
||||
if !isValid(left) || !isValid(right) || !isValid(bottom) {
|
||||
break
|
||||
}
|
||||
|
||||
e1 := sums[left[0]][left[1]][1] - sums[r][c][1]
|
||||
e2 := sums[bottom[0]][bottom[1]][0] - sums[left[0]][left[1]][0]
|
||||
e3 := sums[bottom[0]][bottom[1]][1] - sums[right[0]][right[1]][1]
|
||||
e4 := sums[right[0]][right[1]][0] - sums[r][c][0]
|
||||
addResult(e1 + e2 + e3 + e4 - grid[bottom[0]][bottom[1]] + grid[r][c])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for biggest = biggest[1:]; biggest[0] == 0; {
|
||||
biggest = biggest[1:]
|
||||
}
|
||||
slices.Reverse(biggest)
|
||||
return biggest
|
||||
}
|
||||
|
||||
var _ = getBiggestThree
|
||||
38
solutions/23/q2344/solution.go
Normal file
38
solutions/23/q2344/solution.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// Package q2344 implements a solution for https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/
|
||||
package q2344
|
||||
|
||||
import "slices"
|
||||
|
||||
func gcd(a, b int) int {
|
||||
if a < b {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
for a%b != 0 {
|
||||
a, b = b, a%b
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func minOperations(nums []int, numsDivide []int) int {
|
||||
slices.Sort(nums)
|
||||
|
||||
gcdAll := numsDivide[0]
|
||||
for _, num := range numsDivide {
|
||||
gcdAll = gcd(gcdAll, num)
|
||||
if gcdAll == 1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i := range nums {
|
||||
if nums[i] > gcdAll {
|
||||
break
|
||||
}
|
||||
if gcdAll%nums[i] == 0 {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
var _ = minOperations
|
||||
28
solutions/30/q3070/solution.go
Normal file
28
solutions/30/q3070/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Package q3070 implements a solution for https://leetcode.com/problems/count-submatrices-with-top-left-element-and-sum-less-than-k/
|
||||
package q3070
|
||||
|
||||
func countSubmatrices(grid [][]int, k int) int {
|
||||
cnt := 0
|
||||
for r := range grid {
|
||||
for c := 1; c < len(grid[0]); c++ {
|
||||
grid[r][c] += grid[r][c-1]
|
||||
}
|
||||
}
|
||||
|
||||
for r := range grid {
|
||||
for c := range grid[0] {
|
||||
if r > 0 {
|
||||
grid[r][c] += grid[r-1][c]
|
||||
}
|
||||
|
||||
if grid[r][c] <= k {
|
||||
cnt++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countSubmatrices
|
||||
52
solutions/32/q3212/solution.go
Normal file
52
solutions/32/q3212/solution.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// Package q3212 implements a solution for https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y/
|
||||
package q3212
|
||||
|
||||
type State struct {
|
||||
x, y, dot uint32
|
||||
}
|
||||
|
||||
func (s *State) Add(another *State) {
|
||||
s.x += another.x
|
||||
s.y += another.y
|
||||
s.dot += another.dot
|
||||
}
|
||||
|
||||
func (s *State) Sub(another *State) {
|
||||
s.x -= another.x
|
||||
s.y -= another.y
|
||||
s.dot -= another.dot
|
||||
}
|
||||
|
||||
func numberOfSubmatrices(grid [][]byte) int {
|
||||
cntPrev, cntCur := make([]State, len(grid[0])), make([]State, len(grid[0]))
|
||||
|
||||
submatrices := 0
|
||||
for row := range grid {
|
||||
for col := range grid[0] {
|
||||
st := State{}
|
||||
switch grid[row][col] {
|
||||
case 'X':
|
||||
st.x = 1
|
||||
case 'Y':
|
||||
st.y = 1
|
||||
case '.':
|
||||
st.dot = 1
|
||||
}
|
||||
|
||||
cntCur[col] = st
|
||||
cntCur[col].Add(&cntPrev[col])
|
||||
if col > 0 {
|
||||
cntCur[col].Add(&cntCur[col-1])
|
||||
cntCur[col].Sub(&cntPrev[col-1])
|
||||
}
|
||||
|
||||
if cntCur[col].x > 0 && cntCur[col].y == cntCur[col].x {
|
||||
submatrices++
|
||||
}
|
||||
}
|
||||
cntPrev, cntCur = cntCur, cntPrev
|
||||
}
|
||||
return submatrices
|
||||
}
|
||||
|
||||
var _ = numberOfSubmatrices
|
||||
25
solutions/36/q3612/solution.go
Normal file
25
solutions/36/q3612/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Package q3612 implements a solution for https://leetcode.com/problems/process-string-with-special-operations-i/
|
||||
package q3612
|
||||
|
||||
import "slices"
|
||||
|
||||
func processStr(s string) string {
|
||||
ret := make([]byte, 0, len(s)*2)
|
||||
|
||||
for i := range len(s) {
|
||||
switch s[i] {
|
||||
case '*':
|
||||
ret = ret[:max(len(ret)-1, 0)]
|
||||
case '#':
|
||||
ret = append(ret, ret...)
|
||||
case '%':
|
||||
slices.Reverse(ret)
|
||||
default:
|
||||
ret = append(ret, s[i])
|
||||
}
|
||||
}
|
||||
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
var _ = processStr
|
||||
22
solutions/8/q829/solution.go
Normal file
22
solutions/8/q829/solution.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Package q829 implements a solution for https://leetcode.com/problems/consecutive-numbers-sum/
|
||||
package q829
|
||||
|
||||
import "math"
|
||||
|
||||
func consecutiveNumbersSum(n int) int {
|
||||
maxN := int(math.Sqrt(float64(n) * 2))
|
||||
for maxN*(maxN+1)/2 <= n {
|
||||
maxN++
|
||||
}
|
||||
|
||||
cnt := 1
|
||||
for nNums := maxN - 1; nNums > 1; nNums-- {
|
||||
start := (n*2/nNums + 1 - nNums) / 2
|
||||
if (start+start+nNums-1)*nNums/2 == n {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = consecutiveNumbersSum
|
||||
17
solutions/8/q852/solution.go
Normal file
17
solutions/8/q852/solution.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Package q852 implements a solution for https://leetcode.com/problems/peak-index-in-a-mountain-array/
|
||||
package q852
|
||||
|
||||
func peakIndexInMountainArray(arr []int) int {
|
||||
l, r := 0, len(arr)-1
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
if arr[m] < arr[m+1] { // incrementing
|
||||
l = m + 1
|
||||
} else {
|
||||
r = m
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
var _ = peakIndexInMountainArray
|
||||
Loading…
Add table
Add a link
Reference in a new issue