add new solutions
This commit is contained in:
parent
f249852923
commit
f297e11859
10 changed files with 373 additions and 0 deletions
12
solutions/10/q1009/solution.go
Normal file
12
solutions/10/q1009/solution.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Package q1009 implements a solution for https://leetcode.com/problems/complement-of-base-10-integer/
|
||||||
|
package q1009
|
||||||
|
|
||||||
|
func bitwiseComplement(n int) int {
|
||||||
|
i := 1
|
||||||
|
for i <= n {
|
||||||
|
i <<= 1
|
||||||
|
}
|
||||||
|
return n ^ max(i-1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = bitwiseComplement
|
||||||
44
solutions/15/q1536/solution.go
Normal file
44
solutions/15/q1536/solution.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
// Package q1536 implements a solution for https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/
|
||||||
|
package q1536
|
||||||
|
|
||||||
|
func minSwaps(grid [][]int) int {
|
||||||
|
n := len(grid)
|
||||||
|
|
||||||
|
zeros := grid[0] // reuse memory
|
||||||
|
for i := range grid {
|
||||||
|
z := n - 1
|
||||||
|
for z >= 0 && grid[i][z] == 0 {
|
||||||
|
z--
|
||||||
|
}
|
||||||
|
zeros[i] = n - z - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
steps := 0
|
||||||
|
for row := range n - 1 {
|
||||||
|
needZ := n - row - 1
|
||||||
|
if zeros[row] >= needZ {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find a row with at lease needZ zeros at the end
|
||||||
|
found := -1
|
||||||
|
for i := row + 1; i < n; i++ {
|
||||||
|
if zeros[i] >= needZ {
|
||||||
|
found = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found == -1 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
steps += found - row
|
||||||
|
t := zeros[found]
|
||||||
|
copy(zeros[row+1:found+1], zeros[row:found])
|
||||||
|
zeros[row] = t
|
||||||
|
}
|
||||||
|
|
||||||
|
return steps
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = minSwaps
|
||||||
30
solutions/15/q1545/solution.go
Normal file
30
solutions/15/q1545/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
// Package q1545 implements a solution for https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/
|
||||||
|
package q1545
|
||||||
|
|
||||||
|
var buf = make([]byte, 1<<20)
|
||||||
|
var bufLen = 1
|
||||||
|
|
||||||
|
func init() { buf[0] = '0' }
|
||||||
|
|
||||||
|
func generate() {
|
||||||
|
buf[bufLen] = '1'
|
||||||
|
newTail := bufLen * 2
|
||||||
|
for i := range bufLen {
|
||||||
|
switch buf[i] {
|
||||||
|
case '0':
|
||||||
|
buf[newTail-i] = '1'
|
||||||
|
case '1':
|
||||||
|
buf[newTail-i] = '0'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bufLen = newTail + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func findKthBit(n, k int) byte {
|
||||||
|
for bufLen < k {
|
||||||
|
generate()
|
||||||
|
}
|
||||||
|
return buf[k-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = findKthBit
|
||||||
39
solutions/15/q1582/solution.go
Normal file
39
solutions/15/q1582/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
// Package q1582 implements a solution for https://leetcode.com/problems/special-positions-in-a-binary-matrix/
|
||||||
|
package q1582
|
||||||
|
|
||||||
|
func numSpecial(mat [][]int) int {
|
||||||
|
hPos := make([]int, len(mat))
|
||||||
|
vPos := make([]int, len(mat[0]))
|
||||||
|
for i := range vPos {
|
||||||
|
vPos[i] = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
for row := range mat {
|
||||||
|
hPos[row] = -1
|
||||||
|
for col, val := range mat[row] {
|
||||||
|
if val == 1 {
|
||||||
|
if hPos[row] >= 0 {
|
||||||
|
hPos[row] = -2
|
||||||
|
} else if hPos[row] == -1 {
|
||||||
|
hPos[row] = col
|
||||||
|
}
|
||||||
|
|
||||||
|
if vPos[col] >= 0 {
|
||||||
|
vPos[col] = -2
|
||||||
|
} else if vPos[col] == -1 {
|
||||||
|
vPos[col] = row
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pairs := 0
|
||||||
|
for i := range hPos {
|
||||||
|
if hPos[i] >= 0 && vPos[hPos[i]] == i {
|
||||||
|
pairs++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pairs
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = numSpecial
|
||||||
19
solutions/17/q1758/solution.go
Normal file
19
solutions/17/q1758/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
// Package q1758 implements a solution for https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/
|
||||||
|
package q1758
|
||||||
|
|
||||||
|
func minOperations(s string) int {
|
||||||
|
a, b := 0, 0
|
||||||
|
var alt byte = 1
|
||||||
|
|
||||||
|
for i := range len(s) {
|
||||||
|
if s[i]-'0' == alt {
|
||||||
|
a++
|
||||||
|
} else {
|
||||||
|
b++
|
||||||
|
}
|
||||||
|
alt = alt ^ 1
|
||||||
|
}
|
||||||
|
return min(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = minOperations
|
||||||
25
solutions/17/q1784/solution.go
Normal file
25
solutions/17/q1784/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
// Package q1784 implements a solution for https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/
|
||||||
|
package q1784
|
||||||
|
|
||||||
|
func checkOnesSegment(s string) bool {
|
||||||
|
last := 0
|
||||||
|
segments := 0
|
||||||
|
for i := range len(s) {
|
||||||
|
switch s[i] {
|
||||||
|
case '0':
|
||||||
|
last = 0
|
||||||
|
case '1':
|
||||||
|
if last == 0 {
|
||||||
|
if segments > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
segments++
|
||||||
|
}
|
||||||
|
last = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return segments == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = checkOnesSegment
|
||||||
45
solutions/31/q3129/solution.go
Normal file
45
solutions/31/q3129/solution.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
// Package q3129 implements a solution for https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/
|
||||||
|
package q3129
|
||||||
|
|
||||||
|
const MOD = 1e9 + 7
|
||||||
|
|
||||||
|
func alloc(zeros, ones int) [][][2]int {
|
||||||
|
buf := make([][2]int, (zeros+1)*(ones+1))
|
||||||
|
ret := make([][][2]int, zeros+1)
|
||||||
|
for i := range ret {
|
||||||
|
ret[i] = buf[i*(ones+1) : (i+1)*(ones+1)]
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func numberOfStableArrays(zeros, ones, limit int) int {
|
||||||
|
dp := alloc(zeros, ones)
|
||||||
|
// dimensions: [zeros][ones][last_digit]
|
||||||
|
|
||||||
|
for z := 1; z <= min(limit, zeros); z++ {
|
||||||
|
dp[z][0][0] = 1
|
||||||
|
}
|
||||||
|
for o := 1; o <= min(limit, ones); o++ {
|
||||||
|
dp[0][o][1] = 1
|
||||||
|
}
|
||||||
|
for z := 1; z <= zeros; z++ {
|
||||||
|
for o := 1; o <= ones; o++ {
|
||||||
|
dp[z][o][0] = dp[z-1][o][1] + dp[z-1][o][0]
|
||||||
|
if z > limit {
|
||||||
|
dp[z][o][0] -= dp[z-1-limit][o][1]
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[z][o][1] = dp[z][o-1][0] + dp[z][o-1][1]
|
||||||
|
if o > limit {
|
||||||
|
dp[z][o][1] -= dp[z][o-1-limit][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[z][o][0] = (dp[z][o][0] + MOD) % MOD
|
||||||
|
dp[z][o][1] = (dp[z][o][1] + MOD) % MOD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (dp[zeros][ones][0] + dp[zeros][ones][1]) % MOD
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = numberOfStableArrays
|
||||||
45
solutions/31/q3130/solution.go
Normal file
45
solutions/31/q3130/solution.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
// Package q3130 implements a solution for https://leetcode.com/problems/find-all-possible-stable-binary-arrays-ii/
|
||||||
|
package q3130
|
||||||
|
|
||||||
|
const MOD = 1e9 + 7
|
||||||
|
|
||||||
|
func alloc(zeros, ones int) [][][2]int {
|
||||||
|
buf := make([][2]int, (zeros+1)*(ones+1))
|
||||||
|
ret := make([][][2]int, zeros+1)
|
||||||
|
for i := range ret {
|
||||||
|
ret[i] = buf[i*(ones+1) : (i+1)*(ones+1)]
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func numberOfStableArrays(zeros, ones, limit int) int {
|
||||||
|
dp := alloc(zeros, ones)
|
||||||
|
// dimensions: [zeros][ones][last_digit]
|
||||||
|
|
||||||
|
for z := 1; z <= min(limit, zeros); z++ {
|
||||||
|
dp[z][0][0] = 1
|
||||||
|
}
|
||||||
|
for o := 1; o <= min(limit, ones); o++ {
|
||||||
|
dp[0][o][1] = 1
|
||||||
|
}
|
||||||
|
for z := 1; z <= zeros; z++ {
|
||||||
|
for o := 1; o <= ones; o++ {
|
||||||
|
dp[z][o][0] = dp[z-1][o][1] + dp[z-1][o][0]
|
||||||
|
if z > limit {
|
||||||
|
dp[z][o][0] -= dp[z-1-limit][o][1]
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[z][o][1] = dp[z][o-1][0] + dp[z][o-1][1]
|
||||||
|
if o > limit {
|
||||||
|
dp[z][o][1] -= dp[z][o-1-limit][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[z][o][0] = (dp[z][o][0] + MOD) % MOD
|
||||||
|
dp[z][o][1] = (dp[z][o][1] + MOD) % MOD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (dp[zeros][ones][0] + dp[zeros][ones][1]) % MOD
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = numberOfStableArrays
|
||||||
37
solutions/32/q3296/solution.go
Normal file
37
solutions/32/q3296/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
// Package q3296 implements a solution for https://leetcode.com/problems/minimum-number-of-seconds-to-make-mountain-height-zero/
|
||||||
|
package q3296
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func canFinishIn(mHeight int, wTimes []int, secs int) bool {
|
||||||
|
decHeight := 0
|
||||||
|
|
||||||
|
for _, t := range wTimes {
|
||||||
|
h := int(math.Sqrt(float64(secs) * 2 / float64(t)))
|
||||||
|
if t*h*(h+1)/2 > secs {
|
||||||
|
h--
|
||||||
|
}
|
||||||
|
decHeight += h
|
||||||
|
}
|
||||||
|
return decHeight >= mHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func minNumberOfSeconds(mountainHeight int, workerTimes []int) int64 {
|
||||||
|
worse := slices.Max(workerTimes) * (mountainHeight + 1) * mountainHeight / 2
|
||||||
|
l, r := 0, worse+1
|
||||||
|
|
||||||
|
for l < r {
|
||||||
|
m := (l + r) / 2
|
||||||
|
if canFinishIn(mountainHeight, workerTimes, m) {
|
||||||
|
r = m
|
||||||
|
} else {
|
||||||
|
l = m + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return int64(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = minNumberOfSeconds
|
||||||
77
solutions/36/q3600/solution.go
Normal file
77
solutions/36/q3600/solution.go
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
// Package q3600 implements a solution for https://leetcode.com/problems/maximize-spanning-tree-stability-with-upgrades/
|
||||||
|
package q3600
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func unionFind(unions []int, a int) int {
|
||||||
|
if unions[a] == a {
|
||||||
|
return a
|
||||||
|
} else {
|
||||||
|
root := unionFind(unions, unions[a])
|
||||||
|
unions[a] = root
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func unionJoin(unions []int, a, b int) bool {
|
||||||
|
a, b = min(a, b), max(a, b) // suboptimal dirty impl. ideally, order by size of union
|
||||||
|
uA, uB := unionFind(unions, a), unionFind(unions, b)
|
||||||
|
if uA != uB {
|
||||||
|
unions[uB] = uA
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false // cannot be joined. already in the same set.
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxStability(n int, edges [][]int, k int) int {
|
||||||
|
unions := make([]int, n)
|
||||||
|
for i := range unions {
|
||||||
|
unions[i] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
selected := make([]int, 0, len(edges)+1)
|
||||||
|
nSel, minStr := 0, math.MaxInt
|
||||||
|
for _, e := range edges {
|
||||||
|
if e[3] == 1 { // must include
|
||||||
|
nSel++
|
||||||
|
minStr = min(minStr, e[2])
|
||||||
|
if !unionJoin(unions, e[0], e[1]) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices.SortFunc(edges, func(a, b []int) int {
|
||||||
|
return b[2] - a[2] // sort by strength desc
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := range edges {
|
||||||
|
if nSel >= n-1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if edges[i][3] == 1 { // must include, already done.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
uA, uB := unionFind(unions, edges[i][0]), unionFind(unions, edges[i][1])
|
||||||
|
if uA == uB { // already in the same tree
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
nSel++
|
||||||
|
selected = append(selected, edges[i][2])
|
||||||
|
unionJoin(unions, uA, uB)
|
||||||
|
}
|
||||||
|
|
||||||
|
if nSel != n-1 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
for i := len(selected) - 1; i >= max(0, len(selected)-k); i-- {
|
||||||
|
selected[i] *= 2
|
||||||
|
}
|
||||||
|
selected = append(selected, minStr)
|
||||||
|
return slices.Min(selected)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = maxStability
|
||||||
Loading…
Add table
Add a link
Reference in a new issue