stash 1
This commit is contained in:
parent
51975f3386
commit
987c0bc36f
2 changed files with 54 additions and 0 deletions
36
solutions/1/q110/solution.go
Normal file
36
solutions/1/q110/solution.go
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
package q110
|
||||||
|
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func abs(a int) int {
|
||||||
|
if a < 0 {
|
||||||
|
return -a
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBalancedInternal(node *TreeNode, lvl int) (bool, int) {
|
||||||
|
if node == nil {
|
||||||
|
return true, lvl
|
||||||
|
}
|
||||||
|
|
||||||
|
lBal, lDpth := isBalancedInternal(node.Left, lvl+1)
|
||||||
|
rBal, rDpth := isBalancedInternal(node.Right, lvl+1)
|
||||||
|
dpth := max(lDpth, rDpth)
|
||||||
|
|
||||||
|
if !lBal || !rBal || abs(lDpth-rDpth) > 1 {
|
||||||
|
return false, dpth
|
||||||
|
}
|
||||||
|
return true, dpth
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBalanced(root *TreeNode) bool {
|
||||||
|
ret, _ := isBalancedInternal(root, 0)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = isBalanced
|
||||||
18
solutions/16/q1653/solution.go
Normal file
18
solutions/16/q1653/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package q1653
|
||||||
|
|
||||||
|
func minimumDeletions(s string) int {
|
||||||
|
bestA, bestB := 0, 0
|
||||||
|
|
||||||
|
for i := range len(s) {
|
||||||
|
switch s[i] {
|
||||||
|
case 'a':
|
||||||
|
bestB++
|
||||||
|
case 'b':
|
||||||
|
bestA, bestB = bestA+1, min(bestA, bestB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return min(bestA, bestB)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = minimumDeletions
|
||||||
Loading…
Add table
Add a link
Reference in a new issue