diff --git a/solutions/1/q110/solution.go b/solutions/1/q110/solution.go new file mode 100644 index 0000000..94b73e1 --- /dev/null +++ b/solutions/1/q110/solution.go @@ -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 diff --git a/solutions/16/q1653/solution.go b/solutions/16/q1653/solution.go new file mode 100644 index 0000000..5ccfeca --- /dev/null +++ b/solutions/16/q1653/solution.go @@ -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