add new solutions
This commit is contained in:
parent
f249852923
commit
f297e11859
10 changed files with 373 additions and 0 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue