add new solutions
This commit is contained in:
parent
ee1868a10e
commit
2012261d3d
7 changed files with 205 additions and 0 deletions
27
solutions/14/q1461/solution.go
Normal file
27
solutions/14/q1461/solution.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package q1461
|
||||
|
||||
func hasAllCodes(s string, k int) bool {
|
||||
if len(s) < (1<<k)+k-1 {
|
||||
return false
|
||||
}
|
||||
seen := make([]bool, 1<<k)
|
||||
nSeen := 0
|
||||
|
||||
c := 0
|
||||
for i := range len(s) {
|
||||
c = (c << 1) + int(s[i]-'0')
|
||||
if i+1 >= k {
|
||||
if i >= k {
|
||||
c -= int(s[i-k]-'0') << k
|
||||
}
|
||||
|
||||
if !seen[c] {
|
||||
seen[c] = true
|
||||
nSeen++
|
||||
}
|
||||
}
|
||||
}
|
||||
return nSeen == 1<<k
|
||||
}
|
||||
|
||||
var _ = hasAllCodes
|
||||
20
solutions/16/q1680/solution.go
Normal file
20
solutions/16/q1680/solution.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Package q1680 implements a solution for https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/
|
||||
package q1680
|
||||
|
||||
const MOD int = 1e9 + 7
|
||||
|
||||
func concatenatedBinary(n int) int {
|
||||
ret := 0
|
||||
binLen := 1
|
||||
|
||||
for c := 1; c <= n; c++ {
|
||||
if 1<<binLen <= c {
|
||||
binLen++
|
||||
}
|
||||
|
||||
ret = (ret<<binLen + c) % MOD
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = concatenatedBinary
|
||||
12
solutions/16/q1689/solution.go
Normal file
12
solutions/16/q1689/solution.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Package q1689 implements a solution for https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/
|
||||
package q1689
|
||||
|
||||
func minPartitions(n string) int {
|
||||
maxDigit := 0
|
||||
for i := range len(n) {
|
||||
maxDigit = max(maxDigit, int(n[i]-'0'))
|
||||
}
|
||||
return maxDigit
|
||||
}
|
||||
|
||||
var _ = minPartitions
|
||||
71
solutions/36/q3666/solution.go
Normal file
71
solutions/36/q3666/solution.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// Package q3666 implements a solution for https://leetcode.com/problems/minimum-operations-to-equalize-binary-string/
|
||||
package q3666
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
|
||||
)
|
||||
|
||||
type void struct{}
|
||||
|
||||
const INF = 1<<63 - 1
|
||||
|
||||
var (
|
||||
qBuf = make([]int, 0, 1e5+1)
|
||||
nOpsBuf = make([]int, 0, 1e5+1)
|
||||
)
|
||||
|
||||
func minOperations(s string, k int) int {
|
||||
zeros := strings.Count(s, "0")
|
||||
length := len(s)
|
||||
|
||||
if zeros%2 == 1 && k%2 == 0 {
|
||||
return -1
|
||||
}
|
||||
if k == length && zeros != 0 && zeros != length {
|
||||
return -1
|
||||
}
|
||||
if zeros%k == 0 {
|
||||
return zeros / k
|
||||
}
|
||||
|
||||
nOps := nOpsBuf[:length+1]
|
||||
sets := [2]*rbt.Tree[int, void]{
|
||||
rbt.New[int, void](), rbt.New[int, void](),
|
||||
}
|
||||
for i := range length + 1 {
|
||||
sets[i%2].Put(i, void{})
|
||||
nOps[i] = INF
|
||||
}
|
||||
|
||||
nOps[zeros] = 0
|
||||
sets[zeros%2].Remove(zeros)
|
||||
queue := qBuf[:0]
|
||||
queue = append(queue, zeros)
|
||||
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
z := queue[0]
|
||||
par := (z + k) % 2
|
||||
|
||||
minFlipZ := max(k-(length-z), 0)
|
||||
maxFlipZ := min(k, z)
|
||||
minNewZ := z + k - 2*maxFlipZ
|
||||
maxNewZ := z + k - 2*minFlipZ
|
||||
|
||||
next, found := sets[par].Ceiling(minNewZ)
|
||||
for ; found && next.Key <= maxNewZ; next, found = sets[par].Ceiling(minNewZ) {
|
||||
nextZ := next.Key
|
||||
sets[par].Remove(nextZ)
|
||||
|
||||
nOps[nextZ] = nOps[z] + 1
|
||||
if nextZ%k == 0 {
|
||||
return nOps[nextZ] + nextZ/k
|
||||
}
|
||||
queue = append(queue, nextZ)
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
var _ = minOperations
|
||||
34
solutions/7/q762/solution.go
Normal file
34
solutions/7/q762/solution.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package q762
|
||||
|
||||
var isPrime = [21]bool{}
|
||||
|
||||
func init() {
|
||||
for _, n := range []int{
|
||||
2, 3, 5, 7, 11, 13, 17, 19,
|
||||
} {
|
||||
isPrime[n] = true
|
||||
}
|
||||
}
|
||||
|
||||
func count1s(n int) int {
|
||||
cnt := 0
|
||||
for n > 0 {
|
||||
if n%2 == 1 {
|
||||
cnt++
|
||||
}
|
||||
n >>= 1
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
func countPrimeSetBits(left, right int) int {
|
||||
cnt := 0
|
||||
for n := left; n <= right; n++ {
|
||||
if isPrime[count1s(n)] {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countPrimeSetBits
|
||||
23
solutions/7/q799/solution.go
Normal file
23
solutions/7/q799/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q799
|
||||
|
||||
func champagneTower(poured, query_row, query_glass int) float64 {
|
||||
buf := [100]float64{float64(poured)}
|
||||
for r := range query_row + 1 {
|
||||
var spill float64 = 0
|
||||
for c := range r + 1 {
|
||||
if r == query_row && c == query_glass {
|
||||
return max(buf[c], 1)
|
||||
}
|
||||
|
||||
if buf[c] > 1 {
|
||||
buf[c] = (buf[c] - 1) / 2
|
||||
buf[c], spill = buf[c]+spill, buf[c]
|
||||
} else {
|
||||
buf[c], spill = spill, 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var _ = champagneTower
|
||||
18
solutions/8/q868/solution.go
Normal file
18
solutions/8/q868/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q868
|
||||
|
||||
func binaryGap(n int) int {
|
||||
maxGap := -1
|
||||
gap := -99
|
||||
for n > 0 {
|
||||
if n%2 == 0 {
|
||||
gap++
|
||||
} else {
|
||||
maxGap = max(maxGap, gap)
|
||||
gap = 0
|
||||
}
|
||||
n >>= 1
|
||||
}
|
||||
return maxGap + 1
|
||||
}
|
||||
|
||||
var _ = binaryGap
|
||||
Loading…
Add table
Add a link
Reference in a new issue