From 2012261d3dcd35aeea47d58d773347a3b4c2ab30 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Sun, 1 Mar 2026 13:45:00 +0900 Subject: [PATCH] add new solutions --- solutions/14/q1461/solution.go | 27 +++++++++++++ solutions/16/q1680/solution.go | 20 ++++++++++ solutions/16/q1689/solution.go | 12 ++++++ solutions/36/q3666/solution.go | 71 ++++++++++++++++++++++++++++++++++ solutions/7/q762/solution.go | 34 ++++++++++++++++ solutions/7/q799/solution.go | 23 +++++++++++ solutions/8/q868/solution.go | 18 +++++++++ 7 files changed, 205 insertions(+) create mode 100644 solutions/14/q1461/solution.go create mode 100644 solutions/16/q1680/solution.go create mode 100644 solutions/16/q1689/solution.go create mode 100644 solutions/36/q3666/solution.go create mode 100644 solutions/7/q762/solution.go create mode 100644 solutions/7/q799/solution.go create mode 100644 solutions/8/q868/solution.go diff --git a/solutions/14/q1461/solution.go b/solutions/14/q1461/solution.go new file mode 100644 index 0000000..8e45d8c --- /dev/null +++ b/solutions/14/q1461/solution.go @@ -0,0 +1,27 @@ +package q1461 + +func hasAllCodes(s string, k int) bool { + if len(s) < (1<= k { + if i >= k { + c -= int(s[i-k]-'0') << k + } + + if !seen[c] { + seen[c] = true + nSeen++ + } + } + } + return nSeen == 1< 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 diff --git a/solutions/7/q762/solution.go b/solutions/7/q762/solution.go new file mode 100644 index 0000000..7a3e164 --- /dev/null +++ b/solutions/7/q762/solution.go @@ -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 diff --git a/solutions/7/q799/solution.go b/solutions/7/q799/solution.go new file mode 100644 index 0000000..94e909f --- /dev/null +++ b/solutions/7/q799/solution.go @@ -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 diff --git a/solutions/8/q868/solution.go b/solutions/8/q868/solution.go new file mode 100644 index 0000000..23b3d67 --- /dev/null +++ b/solutions/8/q868/solution.go @@ -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