diff --git a/lcci/04.12.Paths with Sum/Solution.go b/lcci/04.12.Paths with Sum/Solution.go index 1ff246df18838..c8173df1d3e75 100644 --- a/lcci/04.12.Paths with Sum/Solution.go +++ b/lcci/04.12.Paths with Sum/Solution.go @@ -1,25 +1,25 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func pathSum(root *TreeNode, sum int) int { - cnt := map[int]int{0: 1} - var dfs func(*TreeNode, int) int - dfs = func(root *TreeNode, s int) int { - if root == nil { - return 0 - } - s += root.Val - ans := cnt[s-sum] - cnt[s]++ - ans += dfs(root.Left, s) - ans += dfs(root.Right, s) - cnt[s]-- - return ans - } - return dfs(root, 0) +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func pathSum(root *TreeNode, sum int) int { + cnt := map[int]int{0: 1} + var dfs func(*TreeNode, int) int + dfs = func(root *TreeNode, s int) int { + if root == nil { + return 0 + } + s += root.Val + ans := cnt[s-sum] + cnt[s]++ + ans += dfs(root.Left, s) + ans += dfs(root.Right, s) + cnt[s]-- + return ans + } + return dfs(root, 0) } \ No newline at end of file diff --git a/lcci/05.01.Insert Into Bits/Solution.go b/lcci/05.01.Insert Into Bits/Solution.go index 1ac7ade552dc7..bd0c1ff95b81a 100644 --- a/lcci/05.01.Insert Into Bits/Solution.go +++ b/lcci/05.01.Insert Into Bits/Solution.go @@ -1,6 +1,6 @@ -func insertBits(N int, M int, i int, j int) int { - for k := i; k <= j; k++ { - N &= ^(1 << k) - } - return N | M<>i&1 == a && x>>(i-1)&1 == b { - x ^= 1 << i - x ^= 1 << (i - 1) - j, k := 0, i-2 - for j < k { - for j < k && x>>j&1 == b { - j++ - } - for j < k && x>>k&1 == a { - k-- - } - if j < k { - x ^= 1 << j - x ^= 1 << k - } - } - ans[p] = x - break - } - } - } - return ans +func findClosedNumbers(num int) []int { + ans := []int{-1, -1} + dirs := [3]int{0, 1, 0} + for p := 0; p < 2; p++ { + a, b := dirs[p], dirs[p+1] + x := num + for i := 1; i < 31; i++ { + if x>>i&1 == a && x>>(i-1)&1 == b { + x ^= 1 << i + x ^= 1 << (i - 1) + j, k := 0, i-2 + for j < k { + for j < k && x>>j&1 == b { + j++ + } + for j < k && x>>k&1 == a { + k-- + } + if j < k { + x ^= 1 << j + x ^= 1 << k + } + } + ans[p] = x + break + } + } + } + return ans } \ No newline at end of file diff --git a/lcci/08.01.Three Steps Problem/Solution.go b/lcci/08.01.Three Steps Problem/Solution.go index 38b5282653227..905e5ead86930 100644 --- a/lcci/08.01.Three Steps Problem/Solution.go +++ b/lcci/08.01.Three Steps Problem/Solution.go @@ -1,41 +1,41 @@ -const mod = 1e9 + 7 - -func waysToStep(n int) (ans int) { - if n < 4 { - return int(math.Pow(2, float64(n-1))) - } - a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}} - res := pow(a, n-4) - for _, x := range res[0] { - ans = (ans + x) % mod - } - return -} - -func mul(a, b [][]int) [][]int { - m, n := len(a), len(b[0]) - c := make([][]int, m) - for i := range c { - c[i] = make([]int, n) - } - for i := 0; i < m; i++ { - for j := 0; j < n; j++ { - for k := 0; k < len(b); k++ { - c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod - } - } - } - return c -} - -func pow(a [][]int, n int) [][]int { - res := [][]int{{4, 2, 1}} - for n > 0 { - if n&1 == 1 { - res = mul(res, a) - } - a = mul(a, a) - n >>= 1 - } - return res +const mod = 1e9 + 7 + +func waysToStep(n int) (ans int) { + if n < 4 { + return int(math.Pow(2, float64(n-1))) + } + a := [][]int{{1, 1, 0}, {1, 0, 1}, {1, 0, 0}} + res := pow(a, n-4) + for _, x := range res[0] { + ans = (ans + x) % mod + } + return +} + +func mul(a, b [][]int) [][]int { + m, n := len(a), len(b[0]) + c := make([][]int, m) + for i := range c { + c[i] = make([]int, n) + } + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + for k := 0; k < len(b); k++ { + c[i][j] = (c[i][j] + a[i][k]*b[k][j]%mod) % mod + } + } + } + return c +} + +func pow(a [][]int, n int) [][]int { + res := [][]int{{4, 2, 1}} + for n > 0 { + if n&1 == 1 { + res = mul(res, a) + } + a = mul(a, a) + n >>= 1 + } + return res } \ No newline at end of file diff --git a/lcci/08.05.Recursive Mulitply/Solution.go b/lcci/08.05.Recursive Mulitply/Solution.go index 319cdd2304b8d..893e615403ada 100644 --- a/lcci/08.05.Recursive Mulitply/Solution.go +++ b/lcci/08.05.Recursive Mulitply/Solution.go @@ -1,9 +1,9 @@ -func multiply(A int, B int) int { - if B == 1 { - return A - } - if B&1 == 1 { - return (multiply(A, B>>1) << 1) + A - } - return multiply(A, B>>1) << 1 +func multiply(A int, B int) int { + if B == 1 { + return A + } + if B&1 == 1 { + return (multiply(A, B>>1) << 1) + A + } + return multiply(A, B>>1) << 1 } \ No newline at end of file diff --git a/lcci/08.06.Hanota/Solution.go b/lcci/08.06.Hanota/Solution.go index d063010b960ff..957252408c120 100644 --- a/lcci/08.06.Hanota/Solution.go +++ b/lcci/08.06.Hanota/Solution.go @@ -1,21 +1,21 @@ -func hanota(A []int, B []int, C []int) []int { - stk := []Task{{len(A), &A, &B, &C}} - for len(stk) > 0 { - task := stk[len(stk)-1] - stk = stk[:len(stk)-1] - if task.n == 1 { - *task.c = append(*task.c, (*task.a)[len(*task.a)-1]) - *task.a = (*task.a)[:len(*task.a)-1] - } else { - stk = append(stk, Task{task.n - 1, task.b, task.a, task.c}) - stk = append(stk, Task{1, task.a, task.b, task.c}) - stk = append(stk, Task{task.n - 1, task.a, task.c, task.b}) - } - } - return C -} - -type Task struct { - n int - a, b, c *[]int +func hanota(A []int, B []int, C []int) []int { + stk := []Task{{len(A), &A, &B, &C}} + for len(stk) > 0 { + task := stk[len(stk)-1] + stk = stk[:len(stk)-1] + if task.n == 1 { + *task.c = append(*task.c, (*task.a)[len(*task.a)-1]) + *task.a = (*task.a)[:len(*task.a)-1] + } else { + stk = append(stk, Task{task.n - 1, task.b, task.a, task.c}) + stk = append(stk, Task{1, task.a, task.b, task.c}) + stk = append(stk, Task{task.n - 1, task.a, task.c, task.b}) + } + } + return C +} + +type Task struct { + n int + a, b, c *[]int } \ No newline at end of file diff --git a/lcci/08.08.Permutation II/Solution.go b/lcci/08.08.Permutation II/Solution.go index e53c94550e2fb..bdb449b7c9389 100644 --- a/lcci/08.08.Permutation II/Solution.go +++ b/lcci/08.08.Permutation II/Solution.go @@ -1,26 +1,26 @@ -func permutation(S string) (ans []string) { - cs := []byte(S) - sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] }) - t := []byte{} - n := len(cs) - vis := make([]bool, n) - var dfs func(int) - dfs = func(i int) { - if i == n { - ans = append(ans, string(t)) - return - } - for j := 0; j < n; j++ { - if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) { - continue - } - vis[j] = true - t = append(t, cs[j]) - dfs(i + 1) - t = t[:len(t)-1] - vis[j] = false - } - } - dfs(0) - return +func permutation(S string) (ans []string) { + cs := []byte(S) + sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] }) + t := []byte{} + n := len(cs) + vis := make([]bool, n) + var dfs func(int) + dfs = func(i int) { + if i == n { + ans = append(ans, string(t)) + return + } + for j := 0; j < n; j++ { + if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) { + continue + } + vis[j] = true + t = append(t, cs[j]) + dfs(i + 1) + t = t[:len(t)-1] + vis[j] = false + } + } + dfs(0) + return } \ No newline at end of file diff --git a/lcci/08.13.Pile Box/Solution.go b/lcci/08.13.Pile Box/Solution.go index d86c81228e103..456b7c39db0a9 100644 --- a/lcci/08.13.Pile Box/Solution.go +++ b/lcci/08.13.Pile Box/Solution.go @@ -1,18 +1,18 @@ -func pileBox(box [][]int) (ans int) { - sort.Slice(box, func(i, j int) bool { - a, b := box[i], box[j] - return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]) - }) - n := len(box) - f := make([]int, n) - for i := 0; i < n; i++ { - for j := 0; j < i; j++ { - if box[j][1] < box[i][1] && box[j][2] < box[i][2] { - f[i] = max(f[i], f[j]) - } - } - f[i] += box[i][2] - ans = max(ans, f[i]) - } - return +func pileBox(box [][]int) (ans int) { + sort.Slice(box, func(i, j int) bool { + a, b := box[i], box[j] + return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1]) + }) + n := len(box) + f := make([]int, n) + for i := 0; i < n; i++ { + for j := 0; j < i; j++ { + if box[j][1] < box[i][1] && box[j][2] < box[i][2] { + f[i] = max(f[i], f[j]) + } + } + f[i] += box[i][2] + ans = max(ans, f[i]) + } + return } \ No newline at end of file diff --git a/lcci/10.03.Search Rotate Array/Solution.go b/lcci/10.03.Search Rotate Array/Solution.go index 8f81fe1c3392b..4bf9e6b8b8828 100644 --- a/lcci/10.03.Search Rotate Array/Solution.go +++ b/lcci/10.03.Search Rotate Array/Solution.go @@ -1,28 +1,28 @@ -func search(arr []int, target int) int { - l, r := 0, len(arr)-1 - for arr[l] == arr[r] { - r-- - } - for l < r { - mid := (l + r) >> 1 - if arr[mid] > arr[r] { - if arr[l] <= target && target <= arr[mid] { - r = mid - } else { - l = mid + 1 - } - } else if arr[mid] < arr[r] { - if arr[mid] < target && target <= arr[r] { - l = mid + 1 - } else { - r = mid - } - } else { - r-- - } - } - if arr[l] == target { - return l - } - return -1 +func search(arr []int, target int) int { + l, r := 0, len(arr)-1 + for arr[l] == arr[r] { + r-- + } + for l < r { + mid := (l + r) >> 1 + if arr[mid] > arr[r] { + if arr[l] <= target && target <= arr[mid] { + r = mid + } else { + l = mid + 1 + } + } else if arr[mid] < arr[r] { + if arr[mid] < target && target <= arr[r] { + l = mid + 1 + } else { + r = mid + } + } else { + r-- + } + } + if arr[l] == target { + return l + } + return -1 } \ No newline at end of file diff --git a/lcci/16.07.Maximum/Solution.go b/lcci/16.07.Maximum/Solution.go index 0e2fd02ea9adb..cab7f8c3b1a4c 100644 --- a/lcci/16.07.Maximum/Solution.go +++ b/lcci/16.07.Maximum/Solution.go @@ -1,4 +1,4 @@ -func maximum(a int, b int) int { - k := (a - b) >> 63 & 1 - return a*(k^1) + b*k +func maximum(a int, b int) int { + k := (a - b) >> 63 & 1 + return a*(k^1) + b*k } \ No newline at end of file diff --git a/lcci/16.10.Living People/Solution.go b/lcci/16.10.Living People/Solution.go index 0563862cf2e8e..ef68cb29473ae 100644 --- a/lcci/16.10.Living People/Solution.go +++ b/lcci/16.10.Living People/Solution.go @@ -1,19 +1,19 @@ -func maxAliveYear(birth []int, death []int) (ans int) { - base := 1900 - d := [102]int{} - for i, a := range birth { - a -= base - b := death[i] - base - d[a]++ - d[b+1]-- - } - mx, s := 0, 0 - for i, x := range d { - s += x - if mx < s { - mx = s - ans = base + i - } - } - return +func maxAliveYear(birth []int, death []int) (ans int) { + base := 1900 + d := [102]int{} + for i, a := range birth { + a -= base + b := death[i] - base + d[a]++ + d[b+1]-- + } + mx, s := 0, 0 + for i, x := range d { + s += x + if mx < s { + mx = s + ans = base + i + } + } + return } \ No newline at end of file diff --git a/lcci/16.13.Bisect Squares/Solution.go b/lcci/16.13.Bisect Squares/Solution.go index d1860535dfbd1..ef0daf1789ae1 100644 --- a/lcci/16.13.Bisect Squares/Solution.go +++ b/lcci/16.13.Bisect Squares/Solution.go @@ -1,27 +1,27 @@ -func cutSquares(square1 []int, square2 []int) []float64 { - x1, y1 := float64(square1[0])+float64(square1[2])/2, float64(square1[1])+float64(square1[2])/2 - x2, y2 := float64(square2[0])+float64(square2[2])/2, float64(square2[1])+float64(square2[2])/2 - if x1 == x2 { - y3 := math.Min(float64(square1[1]), float64(square2[1])) - y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2])) - return []float64{x1, y3, x2, y4} - } - k := (y2 - y1) / (x2 - x1) - b := y1 - k*x1 - if math.Abs(k) > 1 { - y3 := math.Min(float64(square1[1]), float64(square2[1])) - x3 := (y3 - b) / k - y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2])) - x4 := (y4 - b) / k - if x3 > x4 || (x3 == x4 && y3 > y4) { - return []float64{x4, y4, x3, y3} - } - return []float64{x3, y3, x4, y4} - } else { - x3 := math.Min(float64(square1[0]), float64(square2[0])) - y3 := k*x3 + b - x4 := math.Max(float64(square1[0]+square1[2]), float64(square2[0]+square2[2])) - y4 := k*x4 + b - return []float64{x3, y3, x4, y4} - } +func cutSquares(square1 []int, square2 []int) []float64 { + x1, y1 := float64(square1[0])+float64(square1[2])/2, float64(square1[1])+float64(square1[2])/2 + x2, y2 := float64(square2[0])+float64(square2[2])/2, float64(square2[1])+float64(square2[2])/2 + if x1 == x2 { + y3 := math.Min(float64(square1[1]), float64(square2[1])) + y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2])) + return []float64{x1, y3, x2, y4} + } + k := (y2 - y1) / (x2 - x1) + b := y1 - k*x1 + if math.Abs(k) > 1 { + y3 := math.Min(float64(square1[1]), float64(square2[1])) + x3 := (y3 - b) / k + y4 := math.Max(float64(square1[1]+square1[2]), float64(square2[1]+square2[2])) + x4 := (y4 - b) / k + if x3 > x4 || (x3 == x4 && y3 > y4) { + return []float64{x4, y4, x3, y3} + } + return []float64{x3, y3, x4, y4} + } else { + x3 := math.Min(float64(square1[0]), float64(square2[0])) + y3 := k*x3 + b + x4 := math.Max(float64(square1[0]+square1[2]), float64(square2[0]+square2[2])) + y4 := k*x4 + b + return []float64{x3, y3, x4, y4} + } } \ No newline at end of file diff --git a/lcci/16.16.Sub Sort/Solution.go b/lcci/16.16.Sub Sort/Solution.go index b4954bbcb209f..c997187f42926 100644 --- a/lcci/16.16.Sub Sort/Solution.go +++ b/lcci/16.16.Sub Sort/Solution.go @@ -1,20 +1,20 @@ -func subSort(array []int) []int { - n := len(array) - mi, mx := math.MaxInt32, math.MinInt32 - left, right := -1, -1 - for i, x := range array { - if x < mx { - right = i - } else { - mx = x - } - } - for i := n - 1; i >= 0; i-- { - if array[i] > mi { - left = i - } else { - mi = array[i] - } - } - return []int{left, right} +func subSort(array []int) []int { + n := len(array) + mi, mx := math.MaxInt32, math.MinInt32 + left, right := -1, -1 + for i, x := range array { + if x < mx { + right = i + } else { + mx = x + } + } + for i := n - 1; i >= 0; i-- { + if array[i] > mi { + left = i + } else { + mi = array[i] + } + } + return []int{left, right} } \ No newline at end of file diff --git a/lcci/16.18.Pattern Matching/Solution.go b/lcci/16.18.Pattern Matching/Solution.go index d457e8fe64fe2..a3858012a9dec 100644 --- a/lcci/16.18.Pattern Matching/Solution.go +++ b/lcci/16.18.Pattern Matching/Solution.go @@ -1,45 +1,45 @@ -func patternMatching(pattern string, value string) bool { - cnt := [2]int{} - for _, c := range pattern { - cnt[c-'a']++ - } - n := len(value) - if cnt[0] == 0 { - return n%cnt[1] == 0 && strings.Repeat(value[:n/cnt[1]], cnt[1]) == value - } - if cnt[1] == 0 { - return n%cnt[0] == 0 && strings.Repeat(value[:n/cnt[0]], cnt[0]) == value - } - check := func(la, lb int) bool { - i := 0 - a, b := "", "" - for _, c := range pattern { - if c == 'a' { - if a != "" && value[i:i+la] != a { - return false - } - a = value[i : i+la] - i += la - } else { - if b != "" && value[i:i+lb] != b { - return false - } - b = value[i : i+lb] - i += lb - } - } - return a != b - } - for la := 0; la <= n; la++ { - if la*cnt[0] > n { - break - } - if (n-la*cnt[0])%cnt[1] == 0 { - lb := (n - la*cnt[0]) / cnt[1] - if check(la, lb) { - return true - } - } - } - return false +func patternMatching(pattern string, value string) bool { + cnt := [2]int{} + for _, c := range pattern { + cnt[c-'a']++ + } + n := len(value) + if cnt[0] == 0 { + return n%cnt[1] == 0 && strings.Repeat(value[:n/cnt[1]], cnt[1]) == value + } + if cnt[1] == 0 { + return n%cnt[0] == 0 && strings.Repeat(value[:n/cnt[0]], cnt[0]) == value + } + check := func(la, lb int) bool { + i := 0 + a, b := "", "" + for _, c := range pattern { + if c == 'a' { + if a != "" && value[i:i+la] != a { + return false + } + a = value[i : i+la] + i += la + } else { + if b != "" && value[i:i+lb] != b { + return false + } + b = value[i : i+lb] + i += lb + } + } + return a != b + } + for la := 0; la <= n; la++ { + if la*cnt[0] > n { + break + } + if (n-la*cnt[0])%cnt[1] == 0 { + lb := (n - la*cnt[0]) / cnt[1] + if check(la, lb) { + return true + } + } + } + return false } \ No newline at end of file diff --git a/lcci/16.22.Langtons Ant/Solution.go b/lcci/16.22.Langtons Ant/Solution.go index 128b01dc098ff..bf074a18e4e45 100644 --- a/lcci/16.22.Langtons Ant/Solution.go +++ b/lcci/16.22.Langtons Ant/Solution.go @@ -1,42 +1,42 @@ -func printKMoves(K int) []string { - var x1, y1, x2, y2, x, y, p int - dirs := [5]int{0, 1, 0, -1, 0} - d := "RDLU" - type pair struct{ x, y int } - black := map[pair]bool{} - for K > 0 { - t := pair{x, y} - if black[t] { - delete(black, t) - p = (p + 3) % 4 - } else { - black[t] = true - p = (p + 1) % 4 - } - x += dirs[p] - y += dirs[p+1] - x1 = min(x1, x) - y1 = min(y1, y) - x2 = max(x2, x) - y2 = max(y2, y) - K-- - } - m, n := x2-x1+1, y2-y1+1 - g := make([][]byte, m) - for i := range g { - g[i] = make([]byte, n) - for j := range g[i] { - g[i][j] = '_' - } - } - for t := range black { - i, j := t.x-x1, t.y-y1 - g[i][j] = 'X' - } - g[x-x1][y-y1] = d[p] - ans := make([]string, m) - for i := range ans { - ans[i] = string(g[i]) - } - return ans +func printKMoves(K int) []string { + var x1, y1, x2, y2, x, y, p int + dirs := [5]int{0, 1, 0, -1, 0} + d := "RDLU" + type pair struct{ x, y int } + black := map[pair]bool{} + for K > 0 { + t := pair{x, y} + if black[t] { + delete(black, t) + p = (p + 3) % 4 + } else { + black[t] = true + p = (p + 1) % 4 + } + x += dirs[p] + y += dirs[p+1] + x1 = min(x1, x) + y1 = min(y1, y) + x2 = max(x2, x) + y2 = max(y2, y) + K-- + } + m, n := x2-x1+1, y2-y1+1 + g := make([][]byte, m) + for i := range g { + g[i] = make([]byte, n) + for j := range g[i] { + g[i][j] = '_' + } + } + for t := range black { + i, j := t.x-x1, t.y-y1 + g[i][j] = 'X' + } + g[x-x1][y-y1] = d[p] + ans := make([]string, m) + for i := range ans { + ans[i] = string(g[i]) + } + return ans } \ No newline at end of file diff --git a/lcci/16.26.Calculator/Solution.go b/lcci/16.26.Calculator/Solution.go index 4ebdb08141081..f2057e004e0cc 100644 --- a/lcci/16.26.Calculator/Solution.go +++ b/lcci/16.26.Calculator/Solution.go @@ -1,29 +1,29 @@ -func calculate(s string) (ans int) { - n := len(s) - x := 0 - sign := '+' - stk := []int{} - for i := range s { - if s[i] >= '0' && s[i] <= '9' { - x = x*10 + int(s[i]-'0') - } - if i == n-1 || (s[i] != ' ' && (s[i] < '0' || s[i] > '9')) { - switch sign { - case '+': - stk = append(stk, x) - case '-': - stk = append(stk, -x) - case '*': - stk[len(stk)-1] *= x - case '/': - stk[len(stk)-1] /= x - } - x = 0 - sign = rune(s[i]) - } - } - for _, x := range stk { - ans += x - } - return +func calculate(s string) (ans int) { + n := len(s) + x := 0 + sign := '+' + stk := []int{} + for i := range s { + if s[i] >= '0' && s[i] <= '9' { + x = x*10 + int(s[i]-'0') + } + if i == n-1 || (s[i] != ' ' && (s[i] < '0' || s[i] > '9')) { + switch sign { + case '+': + stk = append(stk, x) + case '-': + stk = append(stk, -x) + case '*': + stk[len(stk)-1] *= x + case '/': + stk[len(stk)-1] /= x + } + x = 0 + sign = rune(s[i]) + } + } + for _, x := range stk { + ans += x + } + return } \ No newline at end of file diff --git a/lcci/17.09.Get Kth Magic Number/README.md b/lcci/17.09.Get Kth Magic Number/README.md index 7e4240c1a50e8..b1eb11b498391 100644 --- a/lcci/17.09.Get Kth Magic Number/README.md +++ b/lcci/17.09.Get Kth Magic Number/README.md @@ -201,8 +201,8 @@ func getKthMagicNumber(k int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.09.Get Kth Magic Number/README_EN.md b/lcci/17.09.Get Kth Magic Number/README_EN.md index 3ca04a08971de..5a0f071063d55 100644 --- a/lcci/17.09.Get Kth Magic Number/README_EN.md +++ b/lcci/17.09.Get Kth Magic Number/README_EN.md @@ -176,8 +176,8 @@ func getKthMagicNumber(k int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.14.Smallest K/README.md b/lcci/17.14.Smallest K/README.md index e1afee06c73c2..fbcbe3fe4e70e 100644 --- a/lcci/17.14.Smallest K/README.md +++ b/lcci/17.14.Smallest K/README.md @@ -162,9 +162,9 @@ func smallestK(arr []int, k int) []int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.14.Smallest K/README_EN.md b/lcci/17.14.Smallest K/README_EN.md index e93c3319e7122..207c51fc97a36 100644 --- a/lcci/17.14.Smallest K/README_EN.md +++ b/lcci/17.14.Smallest K/README_EN.md @@ -145,9 +145,9 @@ func smallestK(arr []int, k int) []int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.20.Continuous Median/README.md b/lcci/17.20.Continuous Median/README.md index 505a0f12dc83b..38c6190d63ae4 100644 --- a/lcci/17.20.Continuous Median/README.md +++ b/lcci/17.20.Continuous Median/README.md @@ -193,9 +193,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.20.Continuous Median/README_EN.md b/lcci/17.20.Continuous Median/README_EN.md index 6f7b58c69c013..f0f0f0c21701a 100644 --- a/lcci/17.20.Continuous Median/README_EN.md +++ b/lcci/17.20.Continuous Median/README_EN.md @@ -181,9 +181,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.20.Continuous Median/Solution.go b/lcci/17.20.Continuous Median/Solution.go index 5e754ca914896..08bef8dd85dc0 100644 --- a/lcci/17.20.Continuous Median/Solution.go +++ b/lcci/17.20.Continuous Median/Solution.go @@ -32,9 +32,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/lcci/17.21.Volume of Histogram/Solution.go b/lcci/17.21.Volume of Histogram/Solution.go index 4d81618b566c2..42a8c6a3001c4 100644 --- a/lcci/17.21.Volume of Histogram/Solution.go +++ b/lcci/17.21.Volume of Histogram/Solution.go @@ -1,17 +1,17 @@ -func trap(height []int) (ans int) { - n := len(height) - if n < 3 { - return 0 - } - left := make([]int, n) - right := make([]int, n) - left[0], right[n-1] = height[0], height[n-1] - for i := 1; i < n; i++ { - left[i] = max(left[i-1], height[i]) - right[n-i-1] = max(right[n-i], height[n-i-1]) - } - for i, h := range height { - ans += min(left[i], right[i]) - h - } - return +func trap(height []int) (ans int) { + n := len(height) + if n < 3 { + return 0 + } + left := make([]int, n) + right := make([]int, n) + left[0], right[n-1] = height[0], height[n-1] + for i := 1; i < n; i++ { + left[i] = max(left[i-1], height[i]) + right[n-i-1] = max(right[n-i], height[n-i-1]) + } + for i, h := range height { + ans += min(left[i], right[i]) - h + } + return } \ No newline at end of file diff --git a/lcci/17.25.Word Rectangle/Solution.go b/lcci/17.25.Word Rectangle/Solution.go index 1fd1dc9f7c619..ca3e63ec1bdbb 100644 --- a/lcci/17.25.Word Rectangle/Solution.go +++ b/lcci/17.25.Word Rectangle/Solution.go @@ -1,73 +1,73 @@ -type Trie struct { - children [26]*Trie - isEnd bool -} - -func newTrie() *Trie { - return &Trie{} -} -func (this *Trie) insert(word string) { - node := this - for _, c := range word { - c -= 'a' - if node.children[c] == nil { - node.children[c] = newTrie() - } - node = node.children[c] - } - node.isEnd = true -} - -func maxRectangle(words []string) (ans []string) { - trie := newTrie() - d := map[int][]string{} - t := []string{} - var maxL, maxS int - for _, w := range words { - maxL = max(maxL, len(w)) - d[len(w)] = append(d[len(w)], w) - trie.insert(w) - } - check := func(mat []string) int { - m, n := len(mat), len(mat[0]) - ans := 1 - for j := 0; j < n; j++ { - node := trie - for i := 0; i < m; i++ { - idx := mat[i][j] - 'a' - if node.children[idx] == nil { - return 0 - } - node = node.children[idx] - } - if !node.isEnd { - ans = 2 - } - } - return ans - } - var dfs func([]string) - dfs = func(ws []string) { - if len(ws[0])*maxL <= maxS || len(t) >= maxL { - return - } - for _, w := range ws { - t = append(t, w) - st := check(t) - if st == 0 { - t = t[:len(t)-1] - continue - } - if st == 1 && maxS < len(t)*len(t[0]) { - maxS = len(t) * len(t[0]) - ans = append([]string{}, t...) - } - dfs(ws) - t = t[:len(t)-1] - } - } - for _, ws := range d { - dfs(ws) - } - return +type Trie struct { + children [26]*Trie + isEnd bool +} + +func newTrie() *Trie { + return &Trie{} +} +func (this *Trie) insert(word string) { + node := this + for _, c := range word { + c -= 'a' + if node.children[c] == nil { + node.children[c] = newTrie() + } + node = node.children[c] + } + node.isEnd = true +} + +func maxRectangle(words []string) (ans []string) { + trie := newTrie() + d := map[int][]string{} + t := []string{} + var maxL, maxS int + for _, w := range words { + maxL = max(maxL, len(w)) + d[len(w)] = append(d[len(w)], w) + trie.insert(w) + } + check := func(mat []string) int { + m, n := len(mat), len(mat[0]) + ans := 1 + for j := 0; j < n; j++ { + node := trie + for i := 0; i < m; i++ { + idx := mat[i][j] - 'a' + if node.children[idx] == nil { + return 0 + } + node = node.children[idx] + } + if !node.isEnd { + ans = 2 + } + } + return ans + } + var dfs func([]string) + dfs = func(ws []string) { + if len(ws[0])*maxL <= maxS || len(t) >= maxL { + return + } + for _, w := range ws { + t = append(t, w) + st := check(t) + if st == 0 { + t = t[:len(t)-1] + continue + } + if st == 1 && maxS < len(t)*len(t[0]) { + maxS = len(t) * len(t[0]) + ans = append([]string{}, t...) + } + dfs(ws) + t = t[:len(t)-1] + } + } + for _, ws := range d { + dfs(ws) + } + return } \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" index dd6287ad30ea9..aba6be58d2713 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" @@ -256,9 +256,9 @@ func getLeastNumbers(arr []int, k int) (ans []int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" index 90d651185d155..afe8eb22789cc 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" @@ -229,9 +229,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.go" index 922c0f93483f1..b16e648a7e8f3 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.go" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.go" @@ -26,9 +26,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" index 2230dc8053b3b..b93d1acc47887 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" @@ -202,10 +202,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.go" index a1047152d9bb5..008196fd54d9e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 039. \347\233\264\346\226\271\345\233\276\346\234\200\345\244\247\347\237\251\345\275\242\351\235\242\347\247\257/Solution.go" @@ -1,33 +1,33 @@ -func largestRectangleArea(heights []int) (ans int) { - n := len(heights) - left := make([]int, n) - right := make([]int, n) - for i := range left { - left[i] = -1 - right[i] = n - } - stk := []int{} - for i, x := range heights { - for len(stk) > 0 && heights[stk[len(stk)-1]] >= x { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - left[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - stk = []int{} - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && heights[stk[len(stk)-1]] >= heights[i] { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - right[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - for i, x := range heights { - ans = max(ans, (right[i]-left[i]-1)*x) - } - return +func largestRectangleArea(heights []int) (ans int) { + n := len(heights) + left := make([]int, n) + right := make([]int, n) + for i := range left { + left[i] = -1 + right[i] = n + } + stk := []int{} + for i, x := range heights { + for len(stk) > 0 && heights[stk[len(stk)-1]] >= x { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + left[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + stk = []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && heights[stk[len(stk)-1]] >= heights[i] { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + right[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + for i, x := range heights { + ans = max(ans, (right[i]-left[i]-1)*x) + } + return } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" index 365fa424763dc..1ebfa76a93752 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/README.md" @@ -194,10 +194,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.go" index 0730efcd2a0db..f36066247883d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.go" @@ -47,10 +47,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index 352894bc72a6c..35ca1f394d03e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -211,11 +211,11 @@ func topKFrequent(nums []int, k int) []int { type pair struct{ v, cnt int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **Rust** diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.go" index 5dfc094dda098..4df836e6d8670 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.go" @@ -20,8 +20,8 @@ func topKFrequent(nums []int, k int) []int { type pair struct{ v, cnt int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/README.md" index 8a220f5596296..fafb7cef6c917 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/README.md" @@ -106,11 +106,11 @@ class Solution { ```go type pairHeap [][]int -func (a pairHeap) Len() int { return len(a) } -func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] } -func (a *pairHeap) Push(x interface{}) { *a = append(*a, x.([]int)) } -func (a *pairHeap) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } +func (a pairHeap) Len() int { return len(a) } +func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] } +func (a *pairHeap) Push(x any) { *a = append(*a, x.([]int)) } +func (a *pairHeap) Pop() any { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { var hp pairHeap diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/Solution.go" index 4f8e649c8b201..71fe3f2ea20c4 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 061. \345\222\214\346\234\200\345\260\217\347\232\204 k \344\270\252\346\225\260\345\257\271/Solution.go" @@ -1,10 +1,10 @@ type pairHeap [][]int -func (a pairHeap) Len() int { return len(a) } -func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] } -func (a *pairHeap) Push(x interface{}) { *a = append(*a, x.([]int)) } -func (a *pairHeap) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } +func (a pairHeap) Len() int { return len(a) } +func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] } +func (a *pairHeap) Push(x any) { *a = append(*a, x.([]int)) } +func (a *pairHeap) Pop() any { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { var hp pairHeap diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" index 24ac446b76435..d874552d4d06e 100644 --- "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" @@ -1,28 +1,28 @@ -func robot(command string, obstacles [][]int, x int, y int) bool { - type pair struct{ i, j int } - vis := map[pair]bool{} - i, j := 0, 0 - vis[pair{0, 0}] = true - for _, c := range command { - if c == 'U' { - j++ - } else { - i++ - } - vis[pair{i, j}] = true - } - k := min(x/i, y/j) - if !vis[pair{x - k*i, y - k*j}] { - return false - } - for _, ob := range obstacles { - if ob[0] > x || ob[1] > y { - continue - } - k := min(ob[0]/i, ob[1]/j) - if vis[pair{ob[0] - k*i, ob[1] - k*j}] { - return false - } - } - return true +func robot(command string, obstacles [][]int, x int, y int) bool { + type pair struct{ i, j int } + vis := map[pair]bool{} + i, j := 0, 0 + vis[pair{0, 0}] = true + for _, c := range command { + if c == 'U' { + j++ + } else { + i++ + } + vis[pair{i, j}] = true + } + k := min(x/i, y/j) + if !vis[pair{x - k*i, y - k*j}] { + return false + } + for _, ob := range obstacles { + if ob[0] > x || ob[1] > y { + continue + } + k := min(ob[0]/i, ob[1]/j) + if vis[pair{ob[0] - k*i, ob[1] - k*j}] { + return false + } + } + return true } \ No newline at end of file diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.go" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.go" index fb8b55bf50ac9..c5254fc271972 100644 --- "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.go" +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.go" @@ -1,13 +1,13 @@ -func numWays(n int, relation [][]int, k int) int { - f := make([]int, n) - f[0] = 1 - for ; k > 0; k-- { - g := make([]int, n) - for _, r := range relation { - a, b := r[0], r[1] - g[b] += f[a] - } - f = g - } - return f[n-1] +func numWays(n int, relation [][]int, k int) int { + f := make([]int, n) + f[0] = 1 + for ; k > 0; k-- { + g := make([]int, n) + for _, r := range relation { + a, b := r[0], r[1] + g[b] += f[a] + } + f = g + } + return f[n-1] } \ No newline at end of file diff --git "a/lcp/LCP 19. \347\247\213\345\217\266\346\224\266\350\227\217\351\233\206/Solution.go" "b/lcp/LCP 19. \347\247\213\345\217\266\346\224\266\350\227\217\351\233\206/Solution.go" index 69960cfbfc453..a0aa622cc4442 100644 --- "a/lcp/LCP 19. \347\247\213\345\217\266\346\224\266\350\227\217\351\233\206/Solution.go" +++ "b/lcp/LCP 19. \347\247\213\345\217\266\346\224\266\350\227\217\351\233\206/Solution.go" @@ -1,25 +1,25 @@ -func minimumOperations(leaves string) int { - n := len(leaves) - f := make([][3]int, n) - inf := 1 << 30 - for i := range f { - f[i] = [3]int{inf, inf, inf} - } - if leaves[0] == 'y' { - f[0][0] = 1 - } else { - f[0][0] = 0 - } - for i := 1; i < n; i++ { - if leaves[i] == 'r' { - f[i][0] = f[i-1][0] - f[i][1] = min(f[i-1][0], f[i-1][1]) + 1 - f[i][2] = min(f[i-1][2], f[i-1][1]) - } else { - f[i][0] = f[i-1][0] + 1 - f[i][1] = min(f[i-1][0], f[i-1][1]) - f[i][2] = min(f[i-1][2], f[i-1][1]) + 1 - } - } - return f[n-1][2] +func minimumOperations(leaves string) int { + n := len(leaves) + f := make([][3]int, n) + inf := 1 << 30 + for i := range f { + f[i] = [3]int{inf, inf, inf} + } + if leaves[0] == 'y' { + f[0][0] = 1 + } else { + f[0][0] = 0 + } + for i := 1; i < n; i++ { + if leaves[i] == 'r' { + f[i][0] = f[i-1][0] + f[i][1] = min(f[i-1][0], f[i-1][1]) + 1 + f[i][2] = min(f[i-1][2], f[i-1][1]) + } else { + f[i][0] = f[i-1][0] + 1 + f[i][1] = min(f[i-1][0], f[i-1][1]) + f[i][2] = min(f[i-1][2], f[i-1][1]) + 1 + } + } + return f[n-1][2] } \ No newline at end of file diff --git "a/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/Solution.go" "b/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/Solution.go" index 90a872f31fca9..052ec12c2f6c4 100644 --- "a/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/Solution.go" +++ "b/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/Solution.go" @@ -1,23 +1,23 @@ -func paintingPlan(n int, k int) (ans int) { - if k == n*n { - return 1 - } - c := make([][]int, n+1) - for i := range c { - c[i] = make([]int, n+1) - } - for i := 0; i <= n; i++ { - c[i][0] = 1 - for j := 1; j <= i; j++ { - c[i][j] = c[i-1][j] + c[i-1][j-1] - } - } - for i := 0; i <= n; i++ { - for j := 0; j <= n; j++ { - if n*(i+j)-i*j == k { - ans += c[n][i] * c[n][j] - } - } - } - return +func paintingPlan(n int, k int) (ans int) { + if k == n*n { + return 1 + } + c := make([][]int, n+1) + for i := range c { + c[i] = make([]int, n+1) + } + for i := 0; i <= n; i++ { + c[i][0] = 1 + for j := 1; j <= i; j++ { + c[i][j] = c[i-1][j] + c[i-1][j-1] + } + } + for i := 0; i <= n; i++ { + for j := 0; j <= n; j++ { + if n*(i+j)-i*j == k { + ans += c[n][i] * c[n][j] + } + } + } + return } \ No newline at end of file diff --git "a/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.go" "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.go" index cc646a8ce453d..de7a13e9851ea 100644 --- "a/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.go" +++ "b/lcp/LCP 39. \346\227\240\344\272\272\346\234\272\346\226\271\351\230\265/Solution.go" @@ -1,19 +1,19 @@ -func minimumSwitchingTimes(source [][]int, target [][]int) (ans int) { - cnt := map[int]int{} - for _, row := range source { - for _, x := range row { - cnt[x]++ - } - } - for _, row := range target { - for _, x := range row { - cnt[x]-- - } - } - for _, v := range cnt { - if v > 0 { - ans += v - } - } - return +func minimumSwitchingTimes(source [][]int, target [][]int) (ans int) { + cnt := map[int]int{} + for _, row := range source { + for _, x := range row { + cnt[x]++ + } + } + for _, row := range target { + for _, x := range row { + cnt[x]-- + } + } + for _, v := range cnt { + if v > 0 { + ans += v + } + } + return } \ No newline at end of file diff --git "a/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.go" "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.go" index 0c0e01c598dad..d569a50205bd7 100644 --- "a/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.go" +++ "b/lcp/LCP 50. \345\256\235\347\237\263\350\241\245\347\273\231/Solution.go" @@ -1,14 +1,14 @@ -func giveGem(gem []int, operations [][]int) int { - for _, op := range operations { - x, y := op[0], op[1] - v := gem[x] >> 1 - gem[y] += v - gem[x] -= v - } - mx, mi := 0, 1<<30 - for _, x := range gem { - mx = max(mx, x) - mi = min(mi, x) - } - return mx - mi +func giveGem(gem []int, operations [][]int) int { + for _, op := range operations { + x, y := op[0], op[1] + v := gem[x] >> 1 + gem[y] += v + gem[x] -= v + } + mx, mi := 0, 1<<30 + for _, x := range gem { + mx = max(mx, x) + mi = min(mi, x) + } + return mx - mi } \ No newline at end of file diff --git "a/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.go" "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.go" index 03f7039cc47e2..e5775a0b007f9 100644 --- "a/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.go" +++ "b/lcp/LCP 51. \347\203\271\351\245\252\346\226\231\347\220\206/Solution.go" @@ -1,26 +1,26 @@ -func perfectMenu(materials []int, cookbooks [][]int, attribute [][]int, limit int) int { - n := len(cookbooks) - ans := -1 - for mask := 0; mask < 1<>i&1 == 1 { - x, y := attribute[i][0], attribute[i][1] - a += x - b += y - for j, v := range cookbooks[i] { - cnt[j] += v - } - } - ok := true - for i := 0; i < 5 && ok; i++ { - ok = cnt[i] <= materials[i] - } - if ok && b >= limit && ans < a { - ans = a - } - } - } - return ans +func perfectMenu(materials []int, cookbooks [][]int, attribute [][]int, limit int) int { + n := len(cookbooks) + ans := -1 + for mask := 0; mask < 1<>i&1 == 1 { + x, y := attribute[i][0], attribute[i][1] + a += x + b += y + for j, v := range cookbooks[i] { + cnt[j] += v + } + } + ok := true + for i := 0; i < 5 && ok; i++ { + ok = cnt[i] <= materials[i] + } + if ok && b >= limit && ans < a { + ans = a + } + } + } + return ans } \ No newline at end of file diff --git "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.go" "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.go" index 5d855d3d0ab1d..778c3db46f952 100644 --- "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.go" +++ "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/Solution.go" @@ -1,31 +1,31 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func getNumber(root *TreeNode, ops [][]int) (ans int) { - rbt := redblacktree.NewWithIntComparator() - var dfs func(root *TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - rbt.Put(root.Val, nil) - dfs(root.Left) - dfs(root.Right) - } - dfs(root) - for i := len(ops) - 1; i >= 0; i-- { - t, x, y := ops[i][0], ops[i][1], ops[i][2] - node, _ := rbt.Ceiling(x) - for node != nil && node.Key.(int) <= y { - rbt.Remove(node.Key) - node, _ = rbt.Ceiling(x) - ans += t - } - } - return +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func getNumber(root *TreeNode, ops [][]int) (ans int) { + rbt := redblacktree.NewWithIntComparator() + var dfs func(root *TreeNode) + dfs = func(root *TreeNode) { + if root == nil { + return + } + rbt.Put(root.Val, nil) + dfs(root.Left) + dfs(root.Right) + } + dfs(root) + for i := len(ops) - 1; i >= 0; i-- { + t, x, y := ops[i][0], ops[i][1], ops[i][2] + node, _ := rbt.Ceiling(x) + for node != nil && node.Key.(int) <= y { + rbt.Remove(node.Key) + node, _ = rbt.Ceiling(x) + ans += t + } + } + return } \ No newline at end of file diff --git a/run_format.py b/run_format.py index eacf01fea13cf..856b39b4387ae 100644 --- a/run_format.py +++ b/run_format.py @@ -4,9 +4,9 @@ import re import black -suffixes = ["md", "py", "java", "c", "cpp", "go", "php", "cs", "rs", "js", "ts", "sql"] +suffixes = ["md", "go"] -code_blocks = ["python", "java", "cpp", "c", "go", "ts", "js", "php", "cs", "rs", "sql"] +code_blocks = ["go"] functions_to_replace = [ "ABS", diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.go b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.go index 0f9acaca6e7f6..983f388793a7c 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.go +++ b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.go @@ -5,7 +5,7 @@ * Next *ListNode * } */ - func mergeKLists(lists []*ListNode) *ListNode { +func mergeKLists(lists []*ListNode) *ListNode { pq := hp{} for _, head := range lists { if head != nil { diff --git a/solution/0000-0099/0061.Rotate List/Solution.go b/solution/0000-0099/0061.Rotate List/Solution.go index d0a91c71da6cd..6d27907948d23 100644 --- a/solution/0000-0099/0061.Rotate List/Solution.go +++ b/solution/0000-0099/0061.Rotate List/Solution.go @@ -1,34 +1,34 @@ -/** - * Definition for singly-linked list. - * type ListNode struct { - * Val int - * Next *ListNode - * } - */ -func rotateRight(head *ListNode, k int) *ListNode { - if head == nil || head.Next == nil { - return head - } - cur := head - n := 0 - for cur != nil { - cur = cur.Next - n++ - } - k %= n - if k == 0 { - return head - } - fast, slow := head, head - for i := 0; i < k; i++ { - fast = fast.Next - } - for fast.Next != nil { - fast = fast.Next - slow = slow.Next - } - ans := slow.Next - slow.Next = nil - fast.Next = head - return ans +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func rotateRight(head *ListNode, k int) *ListNode { + if head == nil || head.Next == nil { + return head + } + cur := head + n := 0 + for cur != nil { + cur = cur.Next + n++ + } + k %= n + if k == 0 { + return head + } + fast, slow := head, head + for i := 0; i < k; i++ { + fast = fast.Next + } + for fast.Next != nil { + fast = fast.Next + slow = slow.Next + } + ans := slow.Next + slow.Next = nil + fast.Next = head + return ans } \ No newline at end of file diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.go b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.go index 52e00131f95b8..ecbc2697cf4bd 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.go +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.go @@ -1,22 +1,22 @@ -func search(nums []int, target int) bool { - l, r := 0, len(nums)-1 - for l < r { - mid := (l + r) >> 1 - if nums[mid] > nums[r] { - if nums[l] <= target && target <= nums[mid] { - r = mid - } else { - l = mid + 1 - } - } else if nums[mid] < nums[r] { - if nums[mid] < target && target <= nums[r] { - l = mid + 1 - } else { - r = mid - } - } else { - r-- - } - } - return nums[l] == target +func search(nums []int, target int) bool { + l, r := 0, len(nums)-1 + for l < r { + mid := (l + r) >> 1 + if nums[mid] > nums[r] { + if nums[l] <= target && target <= nums[mid] { + r = mid + } else { + l = mid + 1 + } + } else if nums[mid] < nums[r] { + if nums[mid] < target && target <= nums[r] { + l = mid + 1 + } else { + r = mid + } + } else { + r-- + } + } + return nums[l] == target } \ No newline at end of file diff --git a/solution/0200-0299/0218.The Skyline Problem/README.md b/solution/0200-0299/0218.The Skyline Problem/README.md index 3ec5031d82335..632d1e7d668fb 100644 --- a/solution/0200-0299/0218.The Skyline Problem/README.md +++ b/solution/0200-0299/0218.The Skyline Problem/README.md @@ -99,12 +99,12 @@ class Solution: type Matrix struct{ left, right, height int } type Queue []Matrix -func (q Queue) Len() int { return len(q) } -func (q Queue) Top() Matrix { return q[0] } -func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } -func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } -func (q *Queue) Push(x interface{}) { *q = append(*q, x.(Matrix)) } -func (q *Queue) Pop() interface{} { +func (q Queue) Len() int { return len(q) } +func (q Queue) Top() Matrix { return q[0] } +func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } +func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } +func (q *Queue) Push(x any) { *q = append(*q, x.(Matrix)) } +func (q *Queue) Pop() any { old, x := *q, (*q)[len(*q)-1] *q = old[:len(old)-1] return x diff --git a/solution/0200-0299/0218.The Skyline Problem/README_EN.md b/solution/0200-0299/0218.The Skyline Problem/README_EN.md index 83962b286d829..2c03646dbbaec 100644 --- a/solution/0200-0299/0218.The Skyline Problem/README_EN.md +++ b/solution/0200-0299/0218.The Skyline Problem/README_EN.md @@ -86,12 +86,12 @@ class Solution: type Matrix struct{ left, right, height int } type Queue []Matrix -func (q Queue) Len() int { return len(q) } -func (q Queue) Top() Matrix { return q[0] } -func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } -func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } -func (q *Queue) Push(x interface{}) { *q = append(*q, x.(Matrix)) } -func (q *Queue) Pop() interface{} { +func (q Queue) Len() int { return len(q) } +func (q Queue) Top() Matrix { return q[0] } +func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } +func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } +func (q *Queue) Push(x any) { *q = append(*q, x.(Matrix)) } +func (q *Queue) Pop() any { old, x := *q, (*q)[len(*q)-1] *q = old[:len(old)-1] return x diff --git a/solution/0200-0299/0218.The Skyline Problem/Solution.go b/solution/0200-0299/0218.The Skyline Problem/Solution.go index 22b207549659f..03219fd3b8c45 100644 --- a/solution/0200-0299/0218.The Skyline Problem/Solution.go +++ b/solution/0200-0299/0218.The Skyline Problem/Solution.go @@ -1,12 +1,12 @@ type Matrix struct{ left, right, height int } type Queue []Matrix -func (q Queue) Len() int { return len(q) } -func (q Queue) Top() Matrix { return q[0] } -func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } -func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } -func (q *Queue) Push(x interface{}) { *q = append(*q, x.(Matrix)) } -func (q *Queue) Pop() interface{} { +func (q Queue) Len() int { return len(q) } +func (q Queue) Top() Matrix { return q[0] } +func (q Queue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } +func (q Queue) Less(i, j int) bool { return q[i].height > q[j].height } +func (q *Queue) Push(x any) { *q = append(*q, x.(Matrix)) } +func (q *Queue) Pop() any { old, x := *q, (*q)[len(*q)-1] *q = old[:len(old)-1] return x diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README.md b/solution/0200-0299/0239.Sliding Window Maximum/README.md index c58551779e872..ce336d828dabf 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README.md @@ -269,9 +269,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v || (a.v == b.v && i < j) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md index ef67e4fe6f677..6db752cc618be 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md @@ -234,9 +234,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v || (a.v == b.v && i < j) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/0200-0299/0264.Ugly Number II/README.md b/solution/0200-0299/0264.Ugly Number II/README.md index 1d140f6be0b7d..28e16f7161d76 100644 --- a/solution/0200-0299/0264.Ugly Number II/README.md +++ b/solution/0200-0299/0264.Ugly Number II/README.md @@ -215,10 +215,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/solution/0200-0299/0264.Ugly Number II/README_EN.md b/solution/0200-0299/0264.Ugly Number II/README_EN.md index 78533aba031e8..e2164af760077 100644 --- a/solution/0200-0299/0264.Ugly Number II/README_EN.md +++ b/solution/0200-0299/0264.Ugly Number II/README_EN.md @@ -187,10 +187,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README.md b/solution/0200-0299/0295.Find Median from Data Stream/README.md index fce1a3a93d23a..b819f5630340d 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README.md @@ -214,9 +214,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md index 8d030eaffccc5..0988873ad085a 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md @@ -199,9 +199,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0200-0299/0295.Find Median from Data Stream/Solution.go b/solution/0200-0299/0295.Find Median from Data Stream/Solution.go index 5e754ca914896..08bef8dd85dc0 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/Solution.go +++ b/solution/0200-0299/0295.Find Median from Data Stream/Solution.go @@ -32,9 +32,9 @@ func (this *MedianFinder) FindMedian() float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0300-0399/0313.Super Ugly Number/README.md b/solution/0300-0399/0313.Super Ugly Number/README.md index 2506890c7fe32..7c8310b6c2d06 100644 --- a/solution/0300-0399/0313.Super Ugly Number/README.md +++ b/solution/0300-0399/0313.Super Ugly Number/README.md @@ -160,8 +160,8 @@ func nthSuperUglyNumber(n int, primes []int) (x int) { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -173,11 +173,11 @@ func (h *hp) Pop() interface{} { type Ugly struct{ value, prime, index int } type Queue []Ugly -func (u Queue) Len() int { return len(u) } -func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] } -func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value } -func (u *Queue) Push(v interface{}) { *u = append(*u, v.(Ugly)) } -func (u *Queue) Pop() interface{} { +func (u Queue) Len() int { return len(u) } +func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] } +func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value } +func (u *Queue) Push(v any) { *u = append(*u, v.(Ugly)) } +func (u *Queue) Pop() any { old, x := *u, (*u)[len(*u)-1] *u = old[:len(old)-1] return x diff --git a/solution/0300-0399/0313.Super Ugly Number/README_EN.md b/solution/0300-0399/0313.Super Ugly Number/README_EN.md index 6c45f85cd7191..8d2ce695512e9 100644 --- a/solution/0300-0399/0313.Super Ugly Number/README_EN.md +++ b/solution/0300-0399/0313.Super Ugly Number/README_EN.md @@ -137,8 +137,8 @@ func nthSuperUglyNumber(n int, primes []int) (x int) { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -150,11 +150,11 @@ func (h *hp) Pop() interface{} { type Ugly struct{ value, prime, index int } type Queue []Ugly -func (u Queue) Len() int { return len(u) } -func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] } -func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value } -func (u *Queue) Push(v interface{}) { *u = append(*u, v.(Ugly)) } -func (u *Queue) Pop() interface{} { +func (u Queue) Len() int { return len(u) } +func (u Queue) Swap(i, j int) { u[i], u[j] = u[j], u[i] } +func (u Queue) Less(i, j int) bool { return u[i].value < u[j].value } +func (u *Queue) Push(v any) { *u = append(*u, v.(Ugly)) } +func (u *Queue) Pop() any { old, x := *u, (*u)[len(*u)-1] *u = old[:len(old)-1] return x diff --git a/solution/0300-0399/0313.Super Ugly Number/Solution.go b/solution/0300-0399/0313.Super Ugly Number/Solution.go index 1ad920115dab6..b2cee164c5e05 100644 --- a/solution/0300-0399/0313.Super Ugly Number/Solution.go +++ b/solution/0300-0399/0313.Super Ugly Number/Solution.go @@ -17,8 +17,8 @@ func nthSuperUglyNumber(n int, primes []int) (x int) { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0300-0399/0321.Create Maximum Number/Solution.go b/solution/0300-0399/0321.Create Maximum Number/Solution.go index f659b98f13d15..04491a70d9bdc 100644 --- a/solution/0300-0399/0321.Create Maximum Number/Solution.go +++ b/solution/0300-0399/0321.Create Maximum Number/Solution.go @@ -1,67 +1,67 @@ -func maxNumber(nums1 []int, nums2 []int, k int) []int { - m, n := len(nums1), len(nums2) - l, r := max(0, k-n), min(k, m) - f := func(nums []int, k int) []int { - n := len(nums) - stk := make([]int, k) - top := -1 - remain := n - k - for _, x := range nums { - for top >= 0 && stk[top] < x && remain > 0 { - top-- - remain-- - } - if top+1 < k { - top++ - stk[top] = x - } else { - remain-- - } - } - return stk - } - - var compare func(nums1, nums2 []int, i, j int) bool - compare = func(nums1, nums2 []int, i, j int) bool { - if i >= len(nums1) { - return false - } - if j >= len(nums2) { - return true - } - if nums1[i] > nums2[j] { - return true - } - if nums1[i] < nums2[j] { - return false - } - return compare(nums1, nums2, i+1, j+1) - } - - merge := func(nums1, nums2 []int) []int { - m, n := len(nums1), len(nums2) - ans := make([]int, m+n) - i, j := 0, 0 - for k := range ans { - if compare(nums1, nums2, i, j) { - ans[k] = nums1[i] - i++ - } else { - ans[k] = nums2[j] - j++ - } - } - return ans - } - - ans := make([]int, k) - for x := l; x <= r; x++ { - arr1 := f(nums1, x) - arr2 := f(nums2, k-x) - arr := merge(arr1, arr2) - if compare(arr, ans, 0, 0) { - ans = arr - } - } - return ans +func maxNumber(nums1 []int, nums2 []int, k int) []int { + m, n := len(nums1), len(nums2) + l, r := max(0, k-n), min(k, m) + f := func(nums []int, k int) []int { + n := len(nums) + stk := make([]int, k) + top := -1 + remain := n - k + for _, x := range nums { + for top >= 0 && stk[top] < x && remain > 0 { + top-- + remain-- + } + if top+1 < k { + top++ + stk[top] = x + } else { + remain-- + } + } + return stk + } + + var compare func(nums1, nums2 []int, i, j int) bool + compare = func(nums1, nums2 []int, i, j int) bool { + if i >= len(nums1) { + return false + } + if j >= len(nums2) { + return true + } + if nums1[i] > nums2[j] { + return true + } + if nums1[i] < nums2[j] { + return false + } + return compare(nums1, nums2, i+1, j+1) + } + + merge := func(nums1, nums2 []int) []int { + m, n := len(nums1), len(nums2) + ans := make([]int, m+n) + i, j := 0, 0 + for k := range ans { + if compare(nums1, nums2, i, j) { + ans[k] = nums1[i] + i++ + } else { + ans[k] = nums2[j] + j++ + } + } + return ans + } + + ans := make([]int, k) + for x := l; x <= r; x++ { + arr1 := f(nums1, x) + arr2 := f(nums2, k-x) + arr := merge(arr1, arr2) + if compare(arr, ans, 0, 0) { + ans = arr + } + } + return ans } \ No newline at end of file diff --git a/solution/0300-0399/0347.Top K Frequent Elements/README.md b/solution/0300-0399/0347.Top K Frequent Elements/README.md index 7d84cb111205b..0bfff9d247b54 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/README.md +++ b/solution/0300-0399/0347.Top K Frequent Elements/README.md @@ -209,11 +209,11 @@ func topKFrequent(nums []int, k int) []int { type pair struct{ v, cnt int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **Rust** diff --git a/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md b/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md index 6e6cb6f509b6b..bf053e8e77c35 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md +++ b/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md @@ -187,11 +187,11 @@ func topKFrequent(nums []int, k int) []int { type pair struct{ v, cnt int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **Rust** diff --git a/solution/0300-0399/0347.Top K Frequent Elements/Solution.go b/solution/0300-0399/0347.Top K Frequent Elements/Solution.go index 5dfc094dda098..4df836e6d8670 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/Solution.go +++ b/solution/0300-0399/0347.Top K Frequent Elements/Solution.go @@ -20,8 +20,8 @@ func topKFrequent(nums []int, k int) []int { type pair struct{ v, cnt int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].cnt < h[j].cnt } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0300-0399/0353.Design Snake Game/Solution.go b/solution/0300-0399/0353.Design Snake Game/Solution.go index e7f794d1c8dac..e24d2f13b80fb 100644 --- a/solution/0300-0399/0353.Design Snake Game/Solution.go +++ b/solution/0300-0399/0353.Design Snake Game/Solution.go @@ -1,55 +1,55 @@ -type SnakeGame struct { - m int - n int - food [][]int - score int - idx int - q []int - vis map[int]bool -} - -func Constructor(width int, height int, food [][]int) SnakeGame { - return SnakeGame{height, width, food, 0, 0, []int{0}, map[int]bool{}} -} - -func (this *SnakeGame) Move(direction string) int { - f := func(i, j int) int { - return i*this.n + j - } - p := this.q[0] - i, j := p/this.n, p%this.n - x, y := i, j - if direction == "U" { - x-- - } else if direction == "D" { - x++ - } else if direction == "L" { - y-- - } else { - y++ - } - if x < 0 || x >= this.m || y < 0 || y >= this.n { - return -1 - } - if this.idx < len(this.food) && x == this.food[this.idx][0] && y == this.food[this.idx][1] { - this.score++ - this.idx++ - } else { - t := this.q[len(this.q)-1] - this.q = this.q[:len(this.q)-1] - this.vis[t] = false - } - cur := f(x, y) - if this.vis[cur] { - return -1 - } - this.q = append([]int{cur}, this.q...) - this.vis[cur] = true - return this.score -} - -/** - * Your SnakeGame object will be instantiated and called as such: - * obj := Constructor(width, height, food); - * param_1 := obj.Move(direction); +type SnakeGame struct { + m int + n int + food [][]int + score int + idx int + q []int + vis map[int]bool +} + +func Constructor(width int, height int, food [][]int) SnakeGame { + return SnakeGame{height, width, food, 0, 0, []int{0}, map[int]bool{}} +} + +func (this *SnakeGame) Move(direction string) int { + f := func(i, j int) int { + return i*this.n + j + } + p := this.q[0] + i, j := p/this.n, p%this.n + x, y := i, j + if direction == "U" { + x-- + } else if direction == "D" { + x++ + } else if direction == "L" { + y-- + } else { + y++ + } + if x < 0 || x >= this.m || y < 0 || y >= this.n { + return -1 + } + if this.idx < len(this.food) && x == this.food[this.idx][0] && y == this.food[this.idx][1] { + this.score++ + this.idx++ + } else { + t := this.q[len(this.q)-1] + this.q = this.q[:len(this.q)-1] + this.vis[t] = false + } + cur := f(x, y) + if this.vis[cur] { + return -1 + } + this.q = append([]int{cur}, this.q...) + this.vis[cur] = true + return this.score +} + +/** + * Your SnakeGame object will be instantiated and called as such: + * obj := Constructor(width, height, food); + * param_1 := obj.Move(direction); */ \ No newline at end of file diff --git a/solution/0300-0399/0358.Rearrange String k Distance Apart/README.md b/solution/0300-0399/0358.Rearrange String k Distance Apart/README.md index 89b849ec3eff0..59a9d6905fe4f 100644 --- a/solution/0300-0399/0358.Rearrange String k Distance Apart/README.md +++ b/solution/0300-0399/0358.Rearrange String k Distance Apart/README.md @@ -197,9 +197,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0300-0399/0358.Rearrange String k Distance Apart/README_EN.md b/solution/0300-0399/0358.Rearrange String k Distance Apart/README_EN.md index 25d543574183f..10e01701d7dc4 100644 --- a/solution/0300-0399/0358.Rearrange String k Distance Apart/README_EN.md +++ b/solution/0300-0399/0358.Rearrange String k Distance Apart/README_EN.md @@ -175,9 +175,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0300-0399/0358.Rearrange String k Distance Apart/Solution.go b/solution/0300-0399/0358.Rearrange String k Distance Apart/Solution.go index 86c0bd48b6f1d..4f30bac82f95a 100644 --- a/solution/0300-0399/0358.Rearrange String k Distance Apart/Solution.go +++ b/solution/0300-0399/0358.Rearrange String k Distance Apart/Solution.go @@ -40,6 +40,6 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/Solution.go b/solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/Solution.go index cd2ad1e04e0cf..472cedd415663 100644 --- a/solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/Solution.go +++ b/solution/0300-0399/0363.Max Sum of Rectangle No Larger Than K/Solution.go @@ -1,25 +1,25 @@ -func maxSumSubmatrix(matrix [][]int, k int) int { - m, n := len(matrix), len(matrix[0]) - const inf = 1 << 30 - ans := -inf - for i := 0; i < m; i++ { - nums := make([]int, n) - for j := i; j < m; j++ { - for h := 0; h < n; h++ { - nums[h] += matrix[j][h] - } - s := 0 - rbt := redblacktree.NewWithIntComparator() - rbt.Put(0, nil) - for _, x := range nums { - s += x - if y, ok := rbt.Ceiling(s - k); ok { - ans = max(ans, s-y.Key.(int)) - } - rbt.Put(s, nil) - } - } - - } - return ans +func maxSumSubmatrix(matrix [][]int, k int) int { + m, n := len(matrix), len(matrix[0]) + const inf = 1 << 30 + ans := -inf + for i := 0; i < m; i++ { + nums := make([]int, n) + for j := i; j < m; j++ { + for h := 0; h < n; h++ { + nums[h] += matrix[j][h] + } + s := 0 + rbt := redblacktree.NewWithIntComparator() + rbt.Put(0, nil) + for _, x := range nums { + s += x + if y, ok := rbt.Ceiling(s - k); ok { + ans = max(ans, s-y.Key.(int)) + } + rbt.Put(s, nil) + } + } + + } + return ans } \ No newline at end of file diff --git a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README.md b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README.md index 27ce4ea23bfe6..98334b98008cc 100644 --- a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README.md +++ b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README.md @@ -165,9 +165,9 @@ func (h hp) Less(i, j int) bool { a, b := h.data[i], h.data[j] return h.nums1[a.i]+h.nums2[a.j] < h.nums1[b.i]+h.nums2[b.j] } -func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } -func (h *hp) Push(v interface{}) { h.data = append(h.data, v.(pair)) } -func (h *hp) Pop() interface{} { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } +func (h *hp) Push(v any) { h.data = append(h.data, v.(pair)) } +func (h *hp) Pop() any { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README_EN.md b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README_EN.md index 9e6f323f10ad2..aa4f941c32b79 100644 --- a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README_EN.md +++ b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/README_EN.md @@ -153,9 +153,9 @@ func (h hp) Less(i, j int) bool { a, b := h.data[i], h.data[j] return h.nums1[a.i]+h.nums2[a.j] < h.nums1[b.i]+h.nums2[b.j] } -func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } -func (h *hp) Push(v interface{}) { h.data = append(h.data, v.(pair)) } -func (h *hp) Pop() interface{} { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } +func (h *hp) Push(v any) { h.data = append(h.data, v.(pair)) } +func (h *hp) Pop() any { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.go b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.go index 76257b87b109b..a0024e4016a4e 100644 --- a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.go +++ b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.go @@ -26,6 +26,6 @@ func (h hp) Less(i, j int) bool { a, b := h.data[i], h.data[j] return h.nums1[a.i]+h.nums2[a.j] < h.nums1[b.i]+h.nums2[b.j] } -func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } -func (h *hp) Push(v interface{}) { h.data = append(h.data, v.(pair)) } -func (h *hp) Pop() interface{} { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Swap(i, j int) { h.data[i], h.data[j] = h.data[j], h.data[i] } +func (h *hp) Push(v any) { h.data = append(h.data, v.(pair)) } +func (h *hp) Pop() any { a := h.data; v := a[len(a)-1]; h.data = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0400-0499/0407.Trapping Rain Water II/README.md b/solution/0400-0499/0407.Trapping Rain Water II/README.md index b5427e1c9fe26..bbf1b73e657a0 100644 --- a/solution/0400-0499/0407.Trapping Rain Water II/README.md +++ b/solution/0400-0499/0407.Trapping Rain Water II/README.md @@ -190,11 +190,11 @@ func trapRainWater(heightMap [][]int) (ans int) { type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md b/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md index 4b8262db7e837..7b5786633f72f 100644 --- a/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md +++ b/solution/0400-0499/0407.Trapping Rain Water II/README_EN.md @@ -170,11 +170,11 @@ func trapRainWater(heightMap [][]int) (ans int) { type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0400-0499/0407.Trapping Rain Water II/Solution.go b/solution/0400-0499/0407.Trapping Rain Water II/Solution.go index 04f93caeb2698..c1d014557a4b3 100644 --- a/solution/0400-0499/0407.Trapping Rain Water II/Solution.go +++ b/solution/0400-0499/0407.Trapping Rain Water II/Solution.go @@ -29,8 +29,8 @@ func trapRainWater(heightMap [][]int) (ans int) { type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0400-0499/0422.Valid Word Square/Solution.go b/solution/0400-0499/0422.Valid Word Square/Solution.go index 9a73a9bce658b..635ed02924dcf 100644 --- a/solution/0400-0499/0422.Valid Word Square/Solution.go +++ b/solution/0400-0499/0422.Valid Word Square/Solution.go @@ -1,11 +1,11 @@ -func validWordSquare(words []string) bool { - m := len(words) - for i, w := range words { - for j := range w { - if j >= m || i >= len(words[j]) || w[j] != words[j][i] { - return false - } - } - } - return true +func validWordSquare(words []string) bool { + m := len(words) + for i, w := range words { + for j := range w { + if j >= m || i >= len(words[j]) || w[j] != words[j][i] { + return false + } + } + } + return true } \ No newline at end of file diff --git a/solution/0400-0499/0446.Arithmetic Slices II - Subsequence/Solution.go b/solution/0400-0499/0446.Arithmetic Slices II - Subsequence/Solution.go index 2809b6cdd2ad5..fdde995297be3 100644 --- a/solution/0400-0499/0446.Arithmetic Slices II - Subsequence/Solution.go +++ b/solution/0400-0499/0446.Arithmetic Slices II - Subsequence/Solution.go @@ -1,15 +1,15 @@ -func numberOfArithmeticSlices(nums []int) (ans int) { - f := make([]map[int]int, len(nums)) - for i := range f { - f[i] = map[int]int{} - } - for i, x := range nums { - for j, y := range nums[:i] { - d := x - y - cnt := f[j][d] - ans += cnt - f[i][d] += cnt + 1 - } - } - return +func numberOfArithmeticSlices(nums []int) (ans int) { + f := make([]map[int]int, len(nums)) + for i := range f { + f[i] = map[int]int{} + } + for i, x := range nums { + for j, y := range nums[:i] { + d := x - y + cnt := f[j][d] + ans += cnt + f[i][d] += cnt + 1 + } + } + return } \ No newline at end of file diff --git a/solution/0400-0499/0449.Serialize and Deserialize BST/Solution.go b/solution/0400-0499/0449.Serialize and Deserialize BST/Solution.go index 6da58c9e4540e..41c7f6d5cb2b5 100644 --- a/solution/0400-0499/0449.Serialize and Deserialize BST/Solution.go +++ b/solution/0400-0499/0449.Serialize and Deserialize BST/Solution.go @@ -1,69 +1,69 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ - -type Codec struct { -} - -func Constructor() Codec { - return Codec{} -} - -// Serializes a tree to a single string. -func (this *Codec) serialize(root *TreeNode) string { - if root == nil { - return "" - } - data := &strings.Builder{} - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - data.WriteString(strconv.Itoa(root.Val)) - data.WriteByte(' ') - dfs(root.Left) - dfs(root.Right) - } - dfs(root) - return data.String()[0 : data.Len()-1] -} - -// Deserializes your encoded data to tree. -func (this *Codec) deserialize(data string) *TreeNode { - if data == "" { - return nil - } - vals := strings.Split(data, " ") - i := 0 - var dfs func(int, int) *TreeNode - dfs = func(mi, mx int) *TreeNode { - if i == len(vals) { - return nil - } - x, _ := strconv.Atoi(vals[i]) - if x < mi || x > mx { - return nil - } - i++ - root := &TreeNode{Val: x} - root.Left = dfs(mi, x) - root.Right = dfs(x, mx) - return root - } - return dfs(math.MinInt64, math.MaxInt64) -} - -/** - * Your Codec object will be instantiated and called as such: - * ser := Constructor() - * deser := Constructor() - * tree := ser.serialize(root) - * ans := deser.deserialize(tree) - * return ans +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +type Codec struct { +} + +func Constructor() Codec { + return Codec{} +} + +// Serializes a tree to a single string. +func (this *Codec) serialize(root *TreeNode) string { + if root == nil { + return "" + } + data := &strings.Builder{} + var dfs func(*TreeNode) + dfs = func(root *TreeNode) { + if root == nil { + return + } + data.WriteString(strconv.Itoa(root.Val)) + data.WriteByte(' ') + dfs(root.Left) + dfs(root.Right) + } + dfs(root) + return data.String()[0 : data.Len()-1] +} + +// Deserializes your encoded data to tree. +func (this *Codec) deserialize(data string) *TreeNode { + if data == "" { + return nil + } + vals := strings.Split(data, " ") + i := 0 + var dfs func(int, int) *TreeNode + dfs = func(mi, mx int) *TreeNode { + if i == len(vals) { + return nil + } + x, _ := strconv.Atoi(vals[i]) + if x < mi || x > mx { + return nil + } + i++ + root := &TreeNode{Val: x} + root.Left = dfs(mi, x) + root.Right = dfs(x, mx) + return root + } + return dfs(math.MinInt64, math.MaxInt64) +} + +/** + * Your Codec object will be instantiated and called as such: + * ser := Constructor() + * deser := Constructor() + * tree := ser.serialize(root) + * ans := deser.deserialize(tree) + * return ans */ \ No newline at end of file diff --git a/solution/0400-0499/0466.Count The Repetitions/Solution.go b/solution/0400-0499/0466.Count The Repetitions/Solution.go index 1a8d51c07019e..46a3e0eda387e 100644 --- a/solution/0400-0499/0466.Count The Repetitions/Solution.go +++ b/solution/0400-0499/0466.Count The Repetitions/Solution.go @@ -1,24 +1,24 @@ -func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) (ans int) { - n := len(s2) - d := make([][2]int, n) - for i := 0; i < n; i++ { - j := i - cnt := 0 - for k := range s1 { - if s1[k] == s2[j] { - j++ - if j == n { - cnt++ - j = 0 - } - } - } - d[i] = [2]int{cnt, j} - } - for j := 0; n1 > 0; n1-- { - ans += d[j][0] - j = d[j][1] - } - ans /= n2 - return +func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) (ans int) { + n := len(s2) + d := make([][2]int, n) + for i := 0; i < n; i++ { + j := i + cnt := 0 + for k := range s1 { + if s1[k] == s2[j] { + j++ + if j == n { + cnt++ + j = 0 + } + } + } + d[i] = [2]int{cnt, j} + } + for j := 0; n1 > 0; n1-- { + ans += d[j][0] + j = d[j][1] + } + ans /= n2 + return } \ No newline at end of file diff --git a/solution/0400-0499/0469.Convex Polygon/Solution.go b/solution/0400-0499/0469.Convex Polygon/Solution.go index b9e4b9840e598..a56406c40cd68 100644 --- a/solution/0400-0499/0469.Convex Polygon/Solution.go +++ b/solution/0400-0499/0469.Convex Polygon/Solution.go @@ -1,18 +1,18 @@ -func isConvex(points [][]int) bool { - n := len(points) - pre, cur := 0, 0 - for i := range points { - x1 := points[(i+1)%n][0] - points[i][0] - y1 := points[(i+1)%n][1] - points[i][1] - x2 := points[(i+2)%n][0] - points[i][0] - y2 := points[(i+2)%n][1] - points[i][1] - cur = x1*y2 - x2*y1 - if cur != 0 { - if cur*pre < 0 { - return false - } - pre = cur - } - } - return true +func isConvex(points [][]int) bool { + n := len(points) + pre, cur := 0, 0 + for i := range points { + x1 := points[(i+1)%n][0] - points[i][0] + y1 := points[(i+1)%n][1] - points[i][1] + x2 := points[(i+2)%n][0] - points[i][0] + y2 := points[(i+2)%n][1] - points[i][1] + cur = x1*y2 - x2*y1 + if cur != 0 { + if cur*pre < 0 { + return false + } + pre = cur + } + } + return true } \ No newline at end of file diff --git a/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.go b/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.go index c09104ffcbefd..43b85554f3432 100644 --- a/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.go +++ b/solution/0400-0499/0470.Implement Rand10() Using Rand7()/Solution.go @@ -1,10 +1,10 @@ -func rand10() int { - for { - i := rand7() - 1 - j := rand7() - x := i*7 + j - if x <= 40 { - return x%10 + 1 - } - } +func rand10() int { + for { + i := rand7() - 1 + j := rand7() + x := i*7 + j + if x <= 40 { + return x%10 + 1 + } + } } \ No newline at end of file diff --git a/solution/0400-0499/0471.Encode String with Shortest Length/Solution.go b/solution/0400-0499/0471.Encode String with Shortest Length/Solution.go index 9deb9b5d58896..da182dac50723 100644 --- a/solution/0400-0499/0471.Encode String with Shortest Length/Solution.go +++ b/solution/0400-0499/0471.Encode String with Shortest Length/Solution.go @@ -1,33 +1,33 @@ -func encode(s string) string { - n := len(s) - f := make([][]string, n) - for i := range f { - f[i] = make([]string, n) - } - g := func(i, j int) string { - t := s[i : j+1] - if len(t) < 5 { - return t - } - k := strings.Index((t + t)[1:], t) + 1 - if k < len(t) { - cnt := len(t) / k - return strconv.Itoa(cnt) + "[" + f[i][i+k-1] + "]" - } - return t - } - for i := n - 1; i >= 0; i-- { - for j := i; j < n; j++ { - f[i][j] = g(i, j) - if j-i+1 > 4 { - for k := i; k < j; k++ { - t := f[i][k] + f[k+1][j] - if len(t) < len(f[i][j]) { - f[i][j] = t - } - } - } - } - } - return f[0][n-1] +func encode(s string) string { + n := len(s) + f := make([][]string, n) + for i := range f { + f[i] = make([]string, n) + } + g := func(i, j int) string { + t := s[i : j+1] + if len(t) < 5 { + return t + } + k := strings.Index((t + t)[1:], t) + 1 + if k < len(t) { + cnt := len(t) / k + return strconv.Itoa(cnt) + "[" + f[i][i+k-1] + "]" + } + return t + } + for i := n - 1; i >= 0; i-- { + for j := i; j < n; j++ { + f[i][j] = g(i, j) + if j-i+1 > 4 { + for k := i; k < j; k++ { + t := f[i][k] + f[k+1][j] + if len(t) < len(f[i][j]) { + f[i][j] = t + } + } + } + } + } + return f[0][n-1] } \ No newline at end of file diff --git a/solution/0400-0499/0480.Sliding Window Median/README.md b/solution/0400-0499/0480.Sliding Window Median/README.md index 32d9c6a75dfe0..51a41a9728d0d 100644 --- a/solution/0400-0499/0480.Sliding Window Median/README.md +++ b/solution/0400-0499/0480.Sliding Window Median/README.md @@ -424,9 +424,9 @@ func medianSlidingWindow(nums []int, k int) []float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0400-0499/0480.Sliding Window Median/README_EN.md b/solution/0400-0499/0480.Sliding Window Median/README_EN.md index f697b2b46395f..2c64219ea1d18 100644 --- a/solution/0400-0499/0480.Sliding Window Median/README_EN.md +++ b/solution/0400-0499/0480.Sliding Window Median/README_EN.md @@ -396,9 +396,9 @@ func medianSlidingWindow(nums []int, k int) []float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0400-0499/0480.Sliding Window Median/Solution.go b/solution/0400-0499/0480.Sliding Window Median/Solution.go index 98a4855b89667..819b902860121 100644 --- a/solution/0400-0499/0480.Sliding Window Median/Solution.go +++ b/solution/0400-0499/0480.Sliding Window Median/Solution.go @@ -88,9 +88,9 @@ func medianSlidingWindow(nums []int, k int) []float64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0500-0599/0502.IPO/README.md b/solution/0500-0599/0502.IPO/README.md index bfc742d589142..5dfc06bcd4841 100644 --- a/solution/0500-0599/0502.IPO/README.md +++ b/solution/0500-0599/0502.IPO/README.md @@ -167,9 +167,9 @@ func findMaximizedCapital(k int, w int, profits []int, capital []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -179,11 +179,11 @@ func (h *hp) Pop() interface{} { type pair struct{ c, p int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0500-0599/0502.IPO/README_EN.md b/solution/0500-0599/0502.IPO/README_EN.md index 7b3067d85fe3c..e117f4084e6d1 100644 --- a/solution/0500-0599/0502.IPO/README_EN.md +++ b/solution/0500-0599/0502.IPO/README_EN.md @@ -150,9 +150,9 @@ func findMaximizedCapital(k int, w int, profits []int, capital []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -162,11 +162,11 @@ func (h *hp) Pop() interface{} { type pair struct{ c, p int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0500-0599/0502.IPO/Solution.go b/solution/0500-0599/0502.IPO/Solution.go index be76e17aedcb7..16336d61c66a7 100644 --- a/solution/0500-0599/0502.IPO/Solution.go +++ b/solution/0500-0599/0502.IPO/Solution.go @@ -19,9 +19,9 @@ func findMaximizedCapital(k int, w int, profits []int, capital []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -31,8 +31,8 @@ func (h *hp) Pop() interface{} { type pair struct{ c, p int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].c < h[j].c } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0500-0599/0529.Minesweeper/Solution.go b/solution/0500-0599/0529.Minesweeper/Solution.go index 0ac5c05008152..7a91bfcab22e1 100644 --- a/solution/0500-0599/0529.Minesweeper/Solution.go +++ b/solution/0500-0599/0529.Minesweeper/Solution.go @@ -1,35 +1,35 @@ -func updateBoard(board [][]byte, click []int) [][]byte { - m, n := len(board), len(board[0]) - i, j := click[0], click[1] - - var dfs func(i, j int) - dfs = func(i, j int) { - cnt := 0 - for x := i - 1; x <= i+1; x++ { - for y := j - 1; y <= j+1; y++ { - if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M' { - cnt++ - } - } - } - if cnt > 0 { - board[i][j] = byte(cnt + '0') - return - } - board[i][j] = 'B' - for x := i - 1; x <= i+1; x++ { - for y := j - 1; y <= j+1; y++ { - if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E' { - dfs(x, y) - } - } - } - } - - if board[i][j] == 'M' { - board[i][j] = 'X' - } else { - dfs(i, j) - } - return board +func updateBoard(board [][]byte, click []int) [][]byte { + m, n := len(board), len(board[0]) + i, j := click[0], click[1] + + var dfs func(i, j int) + dfs = func(i, j int) { + cnt := 0 + for x := i - 1; x <= i+1; x++ { + for y := j - 1; y <= j+1; y++ { + if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M' { + cnt++ + } + } + } + if cnt > 0 { + board[i][j] = byte(cnt + '0') + return + } + board[i][j] = 'B' + for x := i - 1; x <= i+1; x++ { + for y := j - 1; y <= j+1; y++ { + if x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E' { + dfs(x, y) + } + } + } + } + + if board[i][j] == 'M' { + board[i][j] = 'X' + } else { + dfs(i, j) + } + return board } \ No newline at end of file diff --git a/solution/0600-0699/0644.Maximum Average Subarray II/Solution.go b/solution/0600-0699/0644.Maximum Average Subarray II/Solution.go index ea59cbb46d8a6..d77d1019beef7 100644 --- a/solution/0600-0699/0644.Maximum Average Subarray II/Solution.go +++ b/solution/0600-0699/0644.Maximum Average Subarray II/Solution.go @@ -1,37 +1,37 @@ -func findMaxAverage(nums []int, k int) float64 { - eps := 1e-5 - l, r := 1e9, -1e9 - for _, x := range nums { - l = math.Min(l, float64(x)) - r = math.Max(r, float64(x)) - } - check := func(v float64) bool { - s := 0.0 - for _, x := range nums[:k] { - s += float64(x) - v - } - if s >= 0 { - return true - } - t := 0.0 - mi := 0.0 - for i := k; i < len(nums); i++ { - s += float64(nums[i]) - v - t += float64(nums[i-k]) - v - mi = math.Min(mi, t) - if s >= mi { - return true - } - } - return false - } - for r-l >= eps { - mid := (l + r) / 2 - if check(mid) { - l = mid - } else { - r = mid - } - } - return l +func findMaxAverage(nums []int, k int) float64 { + eps := 1e-5 + l, r := 1e9, -1e9 + for _, x := range nums { + l = math.Min(l, float64(x)) + r = math.Max(r, float64(x)) + } + check := func(v float64) bool { + s := 0.0 + for _, x := range nums[:k] { + s += float64(x) - v + } + if s >= 0 { + return true + } + t := 0.0 + mi := 0.0 + for i := k; i < len(nums); i++ { + s += float64(nums[i]) - v + t += float64(nums[i-k]) - v + mi = math.Min(mi, t) + if s >= mi { + return true + } + } + return false + } + for r-l >= eps { + mid := (l + r) / 2 + if check(mid) { + l = mid + } else { + r = mid + } + } + return l } \ No newline at end of file diff --git a/solution/0600-0699/0649.Dota2 Senate/Solution.go b/solution/0600-0699/0649.Dota2 Senate/Solution.go index 7fead77a5081e..e64ce617eeae1 100644 --- a/solution/0600-0699/0649.Dota2 Senate/Solution.go +++ b/solution/0600-0699/0649.Dota2 Senate/Solution.go @@ -1,25 +1,25 @@ -func predictPartyVictory(senate string) string { - n := len(senate) - qr := []int{} - qd := []int{} - for i, c := range senate { - if c == 'R' { - qr = append(qr, i) - } else { - qd = append(qd, i) - } - } - for len(qr) > 0 && len(qd) > 0 { - r, d := qr[0], qd[0] - qr, qd = qr[1:], qd[1:] - if r < d { - qr = append(qr, r+n) - } else { - qd = append(qd, d+n) - } - } - if len(qr) > 0 { - return "Radiant" - } - return "Dire" +func predictPartyVictory(senate string) string { + n := len(senate) + qr := []int{} + qd := []int{} + for i, c := range senate { + if c == 'R' { + qr = append(qr, i) + } else { + qd = append(qd, i) + } + } + for len(qr) > 0 && len(qd) > 0 { + r, d := qr[0], qd[0] + qr, qd = qr[1:], qd[1:] + if r < d { + qr = append(qr, r+n) + } else { + qd = append(qd, d+n) + } + } + if len(qr) > 0 { + return "Radiant" + } + return "Dire" } \ No newline at end of file diff --git a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README.md b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README.md index c3fcb44a58b08..d37ca74bfbfee 100644 --- a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README.md +++ b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README.md @@ -181,8 +181,8 @@ func isPossible(nums []int) bool { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README_EN.md b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README_EN.md index 3eb0fd8945ff1..246a9e0620c08 100644 --- a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README_EN.md +++ b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/README_EN.md @@ -157,8 +157,8 @@ func isPossible(nums []int) bool { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/Solution.go b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/Solution.go index 6e6f301798120..b0e3801ebcccf 100644 --- a/solution/0600-0699/0659.Split Array into Consecutive Subsequences/Solution.go +++ b/solution/0600-0699/0659.Split Array into Consecutive Subsequences/Solution.go @@ -23,8 +23,8 @@ func isPossible(nums []int) bool { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md b/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md index 6b479ffcd3f59..ce6a907e60b4d 100644 --- a/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md +++ b/solution/0700-0799/0703.Kth Largest Element in a Stream/README.md @@ -310,10 +310,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md b/solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md index 5b7b28d0577f7..90db54141153c 100644 --- a/solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md +++ b/solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md @@ -299,10 +299,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.go b/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.go index 0730efcd2a0db..f36066247883d 100644 --- a/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.go +++ b/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.go @@ -47,10 +47,10 @@ type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *IntHeap) Push(x interface{}) { +func (h *IntHeap) Push(x any) { *h = append(*h, x.(int)) } -func (h *IntHeap) Pop() interface{} { +func (h *IntHeap) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/solution/0700-0799/0722.Remove Comments/Solution.go b/solution/0700-0799/0722.Remove Comments/Solution.go index d2a12b1ba8ce8..cbe3260dfdfa8 100644 --- a/solution/0700-0799/0722.Remove Comments/Solution.go +++ b/solution/0700-0799/0722.Remove Comments/Solution.go @@ -1,29 +1,29 @@ -func removeComments(source []string) (ans []string) { - t := []byte{} - blockComment := false - for _, s := range source { - m := len(s) - for i := 0; i < m; i++ { - if blockComment { - if i+1 < m && s[i] == '*' && s[i+1] == '/' { - blockComment = false - i++ - } - } else { - if i+1 < m && s[i] == '/' && s[i+1] == '*' { - blockComment = true - i++ - } else if i+1 < m && s[i] == '/' && s[i+1] == '/' { - break - } else { - t = append(t, s[i]) - } - } - } - if !blockComment && len(t) > 0 { - ans = append(ans, string(t)) - t = []byte{} - } - } - return +func removeComments(source []string) (ans []string) { + t := []byte{} + blockComment := false + for _, s := range source { + m := len(s) + for i := 0; i < m; i++ { + if blockComment { + if i+1 < m && s[i] == '*' && s[i+1] == '/' { + blockComment = false + i++ + } + } else { + if i+1 < m && s[i] == '/' && s[i+1] == '*' { + blockComment = true + i++ + } else if i+1 < m && s[i] == '/' && s[i+1] == '/' { + break + } else { + t = append(t, s[i]) + } + } + } + if !blockComment && len(t) > 0 { + ans = append(ans, string(t)) + t = []byte{} + } + } + return } \ No newline at end of file diff --git a/solution/0700-0799/0727.Minimum Window Subsequence/Solution.go b/solution/0700-0799/0727.Minimum Window Subsequence/Solution.go index c35b21c3b8b98..9ac19df3a5368 100644 --- a/solution/0700-0799/0727.Minimum Window Subsequence/Solution.go +++ b/solution/0700-0799/0727.Minimum Window Subsequence/Solution.go @@ -1,34 +1,34 @@ -func minWindow(s1 string, s2 string) string { - m, n := len(s1), len(s2) - f := make([][]int, m+1) - for i := range f { - f[i] = make([]int, n+1) - } - for i := 1; i <= m; i++ { - for j := 1; j <= n; j++ { - if s1[i-1] == s2[j-1] { - if j == 1 { - f[i][j] = i - } else { - f[i][j] = f[i-1][j-1] - } - } else { - f[i][j] = f[i-1][j] - } - } - } - p, k := 0, m+1 - for i := 1; i <= m; i++ { - if s1[i-1] == s2[n-1] && f[i][n] > 0 { - j := f[i][n] - 1 - if i-j < k { - k = i - j - p = j - } - } - } - if k > m { - return "" - } - return s1[p : p+k] +func minWindow(s1 string, s2 string) string { + m, n := len(s1), len(s2) + f := make([][]int, m+1) + for i := range f { + f[i] = make([]int, n+1) + } + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + if s1[i-1] == s2[j-1] { + if j == 1 { + f[i][j] = i + } else { + f[i][j] = f[i-1][j-1] + } + } else { + f[i][j] = f[i-1][j] + } + } + } + p, k := 0, m+1 + for i := 1; i <= m; i++ { + if s1[i-1] == s2[n-1] && f[i][n] > 0 { + j := f[i][n] - 1 + if i-j < k { + k = i - j + p = j + } + } + } + if k > m { + return "" + } + return s1[p : p+k] } \ No newline at end of file diff --git a/solution/0700-0799/0743.Network Delay Time/README.md b/solution/0700-0799/0743.Network Delay Time/README.md index 1f59500bb5156..a646306024c4f 100644 --- a/solution/0700-0799/0743.Network Delay Time/README.md +++ b/solution/0700-0799/0743.Network Delay Time/README.md @@ -386,9 +386,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func networkDelayTime(times [][]int, n int, k int) int { graph := make([]pairs, n) diff --git a/solution/0700-0799/0743.Network Delay Time/README_EN.md b/solution/0700-0799/0743.Network Delay Time/README_EN.md index e292786324327..ed3d3ba793c8d 100644 --- a/solution/0700-0799/0743.Network Delay Time/README_EN.md +++ b/solution/0700-0799/0743.Network Delay Time/README_EN.md @@ -334,9 +334,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func networkDelayTime(times [][]int, n int, k int) int { graph := make([]pairs, n) diff --git a/solution/0700-0799/0743.Network Delay Time/Solution.go b/solution/0700-0799/0743.Network Delay Time/Solution.go index 61243f205517a..cb3df1b44f5fe 100644 --- a/solution/0700-0799/0743.Network Delay Time/Solution.go +++ b/solution/0700-0799/0743.Network Delay Time/Solution.go @@ -13,9 +13,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func networkDelayTime(times [][]int, n int, k int) int { graph := make([]pairs, n) diff --git a/solution/0700-0799/0750.Number Of Corner Rectangles/Solution.go b/solution/0700-0799/0750.Number Of Corner Rectangles/Solution.go index 1ae14d4bdaa56..b4346ff71c710 100644 --- a/solution/0700-0799/0750.Number Of Corner Rectangles/Solution.go +++ b/solution/0700-0799/0750.Number Of Corner Rectangles/Solution.go @@ -1,19 +1,19 @@ -func countCornerRectangles(grid [][]int) (ans int) { - n := len(grid[0]) - type pair struct{ x, y int } - cnt := map[pair]int{} - for _, row := range grid { - for i, x := range row { - if x == 1 { - for j := i + 1; j < n; j++ { - if row[j] == 1 { - t := pair{i, j} - ans += cnt[t] - cnt[t]++ - } - } - } - } - } - return +func countCornerRectangles(grid [][]int) (ans int) { + n := len(grid[0]) + type pair struct{ x, y int } + cnt := map[pair]int{} + for _, row := range grid { + for i, x := range row { + if x == 1 { + for j := i + 1; j < n; j++ { + if row[j] == 1 { + t := pair{i, j} + ans += cnt[t] + cnt[t]++ + } + } + } + } + } + return } \ No newline at end of file diff --git a/solution/0700-0799/0767.Reorganize String/README.md b/solution/0700-0799/0767.Reorganize String/README.md index d7dd9c162a143..506379fcf5b8e 100644 --- a/solution/0700-0799/0767.Reorganize String/README.md +++ b/solution/0700-0799/0767.Reorganize String/README.md @@ -396,9 +396,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0700-0799/0767.Reorganize String/README_EN.md b/solution/0700-0799/0767.Reorganize String/README_EN.md index 23c119f7598ea..fd38a73342088 100644 --- a/solution/0700-0799/0767.Reorganize String/README_EN.md +++ b/solution/0700-0799/0767.Reorganize String/README_EN.md @@ -356,9 +356,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md b/solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md index 5366269886974..b7cb69c698229 100644 --- a/solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md +++ b/solution/0700-0799/0786.K-th Smallest Prime Fraction/README.md @@ -115,11 +115,11 @@ class Solution { type frac struct{ x, y, i, j int } type hp []frac -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) } -func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } +func (a *hp) Push(x any) { *a = append(*a, x.(frac)) } +func (a *hp) Pop() any { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } func kthSmallestPrimeFraction(arr []int, k int) []int { n := len(arr) diff --git a/solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md b/solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md index 328fa6ee4b238..1e0dd3c7cbc55 100644 --- a/solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md +++ b/solution/0700-0799/0786.K-th Smallest Prime Fraction/README_EN.md @@ -105,11 +105,11 @@ class Solution { type frac struct{ x, y, i, j int } type hp []frac -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) } -func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } +func (a *hp) Push(x any) { *a = append(*a, x.(frac)) } +func (a *hp) Pop() any { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } func kthSmallestPrimeFraction(arr []int, k int) []int { n := len(arr) diff --git a/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.go b/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.go index ca32848a5a7ed..599b3d1c0a489 100644 --- a/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.go +++ b/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.go @@ -1,11 +1,11 @@ type frac struct{ x, y, i, j int } type hp []frac -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(frac)) } -func (a *hp) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].x*a[j].y < a[j].x*a[i].y } +func (a *hp) Push(x any) { *a = append(*a, x.(frac)) } +func (a *hp) Pop() any { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp } func kthSmallestPrimeFraction(arr []int, k int) []int { n := len(arr) diff --git a/solution/0800-0899/0835.Image Overlap/Solution.go b/solution/0800-0899/0835.Image Overlap/Solution.go index a4fa3e0a5d3a3..c86803c356df3 100644 --- a/solution/0800-0899/0835.Image Overlap/Solution.go +++ b/solution/0800-0899/0835.Image Overlap/Solution.go @@ -1,20 +1,20 @@ -func largestOverlap(img1 [][]int, img2 [][]int) (ans int) { - type pair struct{ x, y int } - cnt := map[pair]int{} - for i, row1 := range img1 { - for j, x1 := range row1 { - if x1 == 1 { - for h, row2 := range img2 { - for k, x2 := range row2 { - if x2 == 1 { - t := pair{i - h, j - k} - cnt[t]++ - ans = max(ans, cnt[t]) - } - } - } - } - } - } - return +func largestOverlap(img1 [][]int, img2 [][]int) (ans int) { + type pair struct{ x, y int } + cnt := map[pair]int{} + for i, row1 := range img1 { + for j, x1 := range row1 { + if x1 == 1 { + for h, row2 := range img2 { + for k, x2 := range row2 { + if x2 == 1 { + t := pair{i - h, j - k} + cnt[t]++ + ans = max(ans, cnt[t]) + } + } + } + } + } + } + return } \ No newline at end of file diff --git a/solution/0800-0899/0840.Magic Squares In Grid/Solution.go b/solution/0800-0899/0840.Magic Squares In Grid/Solution.go index be280251ff834..0683c1bf033c4 100644 --- a/solution/0800-0899/0840.Magic Squares In Grid/Solution.go +++ b/solution/0800-0899/0840.Magic Squares In Grid/Solution.go @@ -1,44 +1,44 @@ -func numMagicSquaresInside(grid [][]int) (ans int) { - m, n := len(grid), len(grid[0]) - check := func(i, j int) int { - if i+3 > m || j+3 > n { - return 0 - } - cnt := [16]int{} - row := [3]int{} - col := [3]int{} - a, b := 0, 0 - for x := i; x < i+3; x++ { - for y := j; y < j+3; y++ { - v := grid[x][y] - if v < 1 || v > 9 || cnt[v] > 0 { - return 0 - } - cnt[v]++ - row[x-i] += v - col[y-j] += v - if x-i == y-j { - a += v - } - if x-i == 2-(y-j) { - b += v - } - } - } - if a != b { - return 0 - } - for k := 0; k < 3; k++ { - if row[k] != a || col[k] != a { - return 0 - } - } - return 1 - } - for i := 0; i < m; i++ { - for j := 0; j < n; j++ { - ans += check(i, j) - } - } - return +func numMagicSquaresInside(grid [][]int) (ans int) { + m, n := len(grid), len(grid[0]) + check := func(i, j int) int { + if i+3 > m || j+3 > n { + return 0 + } + cnt := [16]int{} + row := [3]int{} + col := [3]int{} + a, b := 0, 0 + for x := i; x < i+3; x++ { + for y := j; y < j+3; y++ { + v := grid[x][y] + if v < 1 || v > 9 || cnt[v] > 0 { + return 0 + } + cnt[v]++ + row[x-i] += v + col[y-j] += v + if x-i == y-j { + a += v + } + if x-i == 2-(y-j) { + b += v + } + } + } + if a != b { + return 0 + } + for k := 0; k < 3; k++ { + if row[k] != a || col[k] != a { + return 0 + } + } + return 1 + } + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + ans += check(i, j) + } + } + return } \ No newline at end of file diff --git a/solution/0800-0899/0853.Car Fleet/Solution.go b/solution/0800-0899/0853.Car Fleet/Solution.go index 1a83b7f63b4a5..fdc8f3faf214e 100644 --- a/solution/0800-0899/0853.Car Fleet/Solution.go +++ b/solution/0800-0899/0853.Car Fleet/Solution.go @@ -1,17 +1,17 @@ -func carFleet(target int, position []int, speed []int) (ans int) { - n := len(position) - idx := make([]int, n) - for i := range idx { - idx[i] = i - } - sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] }) - var pre float64 - for _, i := range idx { - t := float64(target-position[i]) / float64(speed[i]) - if t > pre { - ans++ - pre = t - } - } - return +func carFleet(target int, position []int, speed []int) (ans int) { + n := len(position) + idx := make([]int, n) + for i := range idx { + idx[i] = i + } + sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] }) + var pre float64 + for _, i := range idx { + t := float64(target-position[i]) / float64(speed[i]) + if t > pre { + ans++ + pre = t + } + } + return } \ No newline at end of file diff --git a/solution/0800-0899/0854.K-Similar Strings/README.md b/solution/0800-0899/0854.K-Similar Strings/README.md index a67314af897f6..02c8790f55e31 100644 --- a/solution/0800-0899/0854.K-Similar Strings/README.md +++ b/solution/0800-0899/0854.K-Similar Strings/README.md @@ -426,9 +426,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v < b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0800-0899/0854.K-Similar Strings/README_EN.md b/solution/0800-0899/0854.K-Similar Strings/README_EN.md index 3bdcb56e147ff..9d2c884378af8 100644 --- a/solution/0800-0899/0854.K-Similar Strings/README_EN.md +++ b/solution/0800-0899/0854.K-Similar Strings/README_EN.md @@ -393,9 +393,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v < b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0800-0899/0855.Exam Room/README.md b/solution/0800-0899/0855.Exam Room/README.md index daba1bded6769..13369b9394393 100644 --- a/solution/0800-0899/0855.Exam Room/README.md +++ b/solution/0800-0899/0855.Exam Room/README.md @@ -264,7 +264,7 @@ func Constructor(n int) ExamRoom { } return (s[1] - s[0]) >> 1 } - cmp := func(a, b interface{}) int { + cmp := func(a, b any) int { x, y := a.([]int), b.([]int) d1, d2 := dist(x), dist(y) if d1 == d2 { diff --git a/solution/0800-0899/0855.Exam Room/README_EN.md b/solution/0800-0899/0855.Exam Room/README_EN.md index 011efe48162e2..688970f00ff08 100644 --- a/solution/0800-0899/0855.Exam Room/README_EN.md +++ b/solution/0800-0899/0855.Exam Room/README_EN.md @@ -260,7 +260,7 @@ func Constructor(n int) ExamRoom { } return (s[1] - s[0]) >> 1 } - cmp := func(a, b interface{}) int { + cmp := func(a, b any) int { x, y := a.([]int), b.([]int) d1, d2 := dist(x), dist(y) if d1 == d2 { diff --git a/solution/0800-0899/0855.Exam Room/Solution.go b/solution/0800-0899/0855.Exam Room/Solution.go index 0c2dd1e69adc0..2cce5d0226921 100644 --- a/solution/0800-0899/0855.Exam Room/Solution.go +++ b/solution/0800-0899/0855.Exam Room/Solution.go @@ -12,7 +12,7 @@ func Constructor(n int) ExamRoom { } return (s[1] - s[0]) >> 1 } - cmp := func(a, b interface{}) int { + cmp := func(a, b any) int { x, y := a.([]int), b.([]int) d1, d2 := dist(x), dist(y) if d1 == d2 { diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md index 65ee9a4deeb64..3658cea3b1446 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md @@ -183,8 +183,8 @@ type pair struct { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md index 3efefc6d1cafc..29a4b7da1d20f 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md @@ -160,8 +160,8 @@ type pair struct { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go index f5ffa150cf129..cd286f89e00ca 100644 --- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go +++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/Solution.go @@ -25,8 +25,8 @@ type pair struct { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0858.Mirror Reflection/Solution.go b/solution/0800-0899/0858.Mirror Reflection/Solution.go index 2206b7f77ceba..5b97dc2edeaf2 100644 --- a/solution/0800-0899/0858.Mirror Reflection/Solution.go +++ b/solution/0800-0899/0858.Mirror Reflection/Solution.go @@ -1,19 +1,19 @@ -func mirrorReflection(p int, q int) int { - g := gcd(p, q) - p = (p / g) % 2 - q = (q / g) % 2 - if p == 1 && q == 1 { - return 1 - } - if p == 1 { - return 0 - } - return 2 -} - -func gcd(a, b int) int { - if b == 0 { - return a - } - return gcd(b, a%b) +func mirrorReflection(p int, q int) int { + g := gcd(p, q) + p = (p / g) % 2 + q = (q / g) % 2 + if p == 1 && q == 1 { + return 1 + } + if p == 1 { + return 0 + } + return 2 +} + +func gcd(a, b int) int { + if b == 0 { + return a + } + return gcd(b, a%b) } \ No newline at end of file diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md index 4572a7f6afe84..a2b3b6bc80196 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md @@ -177,9 +177,9 @@ func minRefuelStops(target int, startFuel int, stations [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md index 77e463440126d..efd3741cd8a7c 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md @@ -160,9 +160,9 @@ func minRefuelStops(target int, startFuel int, stations [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.go b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.go index 2a180d61dd15e..7c5865f281df5 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.go +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.go @@ -21,9 +21,9 @@ func minRefuelStops(target int, startFuel int, stations [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README.md b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README.md index 5de2a016f5eac..8b5986fb6c576 100644 --- a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README.md +++ b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README.md @@ -245,11 +245,11 @@ func reachableNodes(edges [][]int, maxMoves int, n int) (ans int) { type pair struct{ v, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README_EN.md b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README_EN.md index 709e6a87b4a73..c0c7f3f6568c1 100644 --- a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README_EN.md +++ b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/README_EN.md @@ -211,11 +211,11 @@ func reachableNodes(edges [][]int, maxMoves int, n int) (ans int) { type pair struct{ v, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/Solution.go b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/Solution.go index aaaa7f1210a50..8dc85a00238f3 100644 --- a/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/Solution.go +++ b/solution/0800-0899/0882.Reachable Nodes In Subdivided Graph/Solution.go @@ -39,8 +39,8 @@ func reachableNodes(edges [][]int, maxMoves int, n int) (ans int) { type pair struct{ v, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/0800-0899/0895.Maximum Frequency Stack/README.md b/solution/0800-0899/0895.Maximum Frequency Stack/README.md index 709a96bdbc641..54582afc961e7 100644 --- a/solution/0800-0899/0895.Maximum Frequency Stack/README.md +++ b/solution/0800-0899/0895.Maximum Frequency Stack/README.md @@ -300,9 +300,9 @@ func (h hp) Len() int { return len(h) } func (h hp) Less(i, j int) bool { return h[i].cnt > h[j].cnt || h[i].cnt == h[j].cnt && h[i].ts > h[j].ts } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } /** * Your FreqStack object will be instantiated and called as such: diff --git a/solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md b/solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md index 5ab6ec178d88f..8b572aacde013 100644 --- a/solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md +++ b/solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md @@ -273,9 +273,9 @@ func (h hp) Len() int { return len(h) } func (h hp) Less(i, j int) bool { return h[i].cnt > h[j].cnt || h[i].cnt == h[j].cnt && h[i].ts > h[j].ts } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } /** * Your FreqStack object will be instantiated and called as such: diff --git a/solution/0900-0999/0903.Valid Permutations for DI Sequence/Solution.go b/solution/0900-0999/0903.Valid Permutations for DI Sequence/Solution.go index c83ba9c3ae9fb..391b2e74a02dd 100644 --- a/solution/0900-0999/0903.Valid Permutations for DI Sequence/Solution.go +++ b/solution/0900-0999/0903.Valid Permutations for DI Sequence/Solution.go @@ -1,26 +1,26 @@ -func numPermsDISequence(s string) (ans int) { - const mod = 1e9 + 7 - n := len(s) - f := make([]int, n+1) - f[0] = 1 - for i := 1; i <= n; i++ { - pre := 0 - g := make([]int, n+1) - if s[i-1] == 'D' { - for j := i; j >= 0; j-- { - pre = (pre + f[j]) % mod - g[j] = pre - } - } else { - for j := 0; j <= i; j++ { - g[j] = pre - pre = (pre + f[j]) % mod - } - } - f = g - } - for j := 0; j <= n; j++ { - ans = (ans + f[j]) % mod - } - return +func numPermsDISequence(s string) (ans int) { + const mod = 1e9 + 7 + n := len(s) + f := make([]int, n+1) + f[0] = 1 + for i := 1; i <= n; i++ { + pre := 0 + g := make([]int, n+1) + if s[i-1] == 'D' { + for j := i; j >= 0; j-- { + pre = (pre + f[j]) % mod + g[j] = pre + } + } else { + for j := 0; j <= i; j++ { + g[j] = pre + pre = (pre + f[j]) % mod + } + } + f = g + } + for j := 0; j <= n; j++ { + ans = (ans + f[j]) % mod + } + return } \ No newline at end of file diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.go b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.go index 1bb572d3bca1d..7e70043457eeb 100644 --- a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.go +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.go @@ -1,9 +1,9 @@ -func repeatedNTimes(nums []int) int { - s := map[int]bool{} - for i := 0; ; i++ { - if s[nums[i]] { - return nums[i] - } - s[nums[i]] = true - } +func repeatedNTimes(nums []int) int { + s := map[int]bool{} + for i := 0; ; i++ { + if s[nums[i]] { + return nums[i] + } + s[nums[i]] = true + } } \ No newline at end of file diff --git a/solution/0900-0999/0963.Minimum Area Rectangle II/Solution.go b/solution/0900-0999/0963.Minimum Area Rectangle II/Solution.go index 16bff4ee8b06b..d98c335f38e97 100644 --- a/solution/0900-0999/0963.Minimum Area Rectangle II/Solution.go +++ b/solution/0900-0999/0963.Minimum Area Rectangle II/Solution.go @@ -1,36 +1,36 @@ -func minAreaFreeRect(points [][]int) float64 { - n := len(points) - f := func(x, y int) int { - return x*40001 + y - } - s := map[int]bool{} - for _, p := range points { - s[f(p[0], p[1])] = true - } - ans := 1e20 - for i := 0; i < n; i++ { - x1, y1 := points[i][0], points[i][1] - for j := 0; j < n; j++ { - if j != i { - x2, y2 := points[j][0], points[j][1] - for k := j + 1; k < n; k++ { - if k != i { - x3, y3 := points[k][0], points[k][1] - x4, y4 := x2-x1+x3, y2-y1+y3 - if s[f(x4, y4)] { - if (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1) == 0 { - ww := (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) - hh := (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) - ans = math.Min(ans, math.Sqrt(float64(ww*hh))) - } - } - } - } - } - } - } - if ans == 1e20 { - return 0 - } - return ans +func minAreaFreeRect(points [][]int) float64 { + n := len(points) + f := func(x, y int) int { + return x*40001 + y + } + s := map[int]bool{} + for _, p := range points { + s[f(p[0], p[1])] = true + } + ans := 1e20 + for i := 0; i < n; i++ { + x1, y1 := points[i][0], points[i][1] + for j := 0; j < n; j++ { + if j != i { + x2, y2 := points[j][0], points[j][1] + for k := j + 1; k < n; k++ { + if k != i { + x3, y3 := points[k][0], points[k][1] + x4, y4 := x2-x1+x3, y2-y1+y3 + if s[f(x4, y4)] { + if (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1) == 0 { + ww := (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + hh := (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1) + ans = math.Min(ans, math.Sqrt(float64(ww*hh))) + } + } + } + } + } + } + } + if ans == 1e20 { + return 0 + } + return ans } \ No newline at end of file diff --git a/solution/0900-0999/0964.Least Operators to Express Number/Solution.go b/solution/0900-0999/0964.Least Operators to Express Number/Solution.go index 212bd6ebe6eca..96ded9fafbf29 100644 --- a/solution/0900-0999/0964.Least Operators to Express Number/Solution.go +++ b/solution/0900-0999/0964.Least Operators to Express Number/Solution.go @@ -1,25 +1,25 @@ -func leastOpsExpressTarget(x int, target int) int { - f := map[int]int{} - var dfs func(int) int - dfs = func(v int) int { - if x > v { - return min(v*2-1, 2*(x-v)) - } - if val, ok := f[v]; ok { - return val - } - k := 2 - y := x * x - for y < v { - y *= x - k++ - } - ans := k - 1 + dfs(v-y/x) - if y-v < v { - ans = min(ans, k+dfs(y-v)) - } - f[v] = ans - return ans - } - return dfs(target) +func leastOpsExpressTarget(x int, target int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(v int) int { + if x > v { + return min(v*2-1, 2*(x-v)) + } + if val, ok := f[v]; ok { + return val + } + k := 2 + y := x * x + for y < v { + y *= x + k++ + } + ans := k - 1 + dfs(v-y/x) + if y-v < v { + ans = min(ans, k+dfs(y-v)) + } + f[v] = ans + return ans + } + return dfs(target) } \ No newline at end of file diff --git a/solution/0900-0999/0971.Flip Binary Tree To Match Preorder Traversal/Solution.go b/solution/0900-0999/0971.Flip Binary Tree To Match Preorder Traversal/Solution.go index 314f3cb915b80..4a14668e56cc8 100644 --- a/solution/0900-0999/0971.Flip Binary Tree To Match Preorder Traversal/Solution.go +++ b/solution/0900-0999/0971.Flip Binary Tree To Match Preorder Traversal/Solution.go @@ -1,37 +1,37 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func flipMatchVoyage(root *TreeNode, voyage []int) []int { - i := 0 - ok := true - ans := []int{} - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil || !ok { - return - } - if root.Val != voyage[i] { - ok = false - return - } - i++ - if root.Left == nil || root.Left.Val == voyage[i] { - dfs(root.Left) - dfs(root.Right) - } else { - ans = append(ans, root.Val) - dfs(root.Right) - dfs(root.Left) - } - } - dfs(root) - if !ok { - return []int{-1} - } - return ans +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func flipMatchVoyage(root *TreeNode, voyage []int) []int { + i := 0 + ok := true + ans := []int{} + var dfs func(*TreeNode) + dfs = func(root *TreeNode) { + if root == nil || !ok { + return + } + if root.Val != voyage[i] { + ok = false + return + } + i++ + if root.Left == nil || root.Left.Val == voyage[i] { + dfs(root.Left) + dfs(root.Right) + } else { + ans = append(ans, root.Val) + dfs(root.Right) + dfs(root.Left) + } + } + dfs(root) + if !ok { + return []int{-1} + } + return ans } \ No newline at end of file diff --git a/solution/1000-1099/1046.Last Stone Weight/README.md b/solution/1000-1099/1046.Last Stone Weight/README.md index ff5afdce1399d..2ed7640557f42 100644 --- a/solution/1000-1099/1046.Last Stone Weight/README.md +++ b/solution/1000-1099/1046.Last Stone Weight/README.md @@ -136,9 +136,9 @@ func lastStoneWeight(stones []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1000-1099/1046.Last Stone Weight/README_EN.md b/solution/1000-1099/1046.Last Stone Weight/README_EN.md index 00225d85ad74a..b5246edbc5624 100644 --- a/solution/1000-1099/1046.Last Stone Weight/README_EN.md +++ b/solution/1000-1099/1046.Last Stone Weight/README_EN.md @@ -128,9 +128,9 @@ func lastStoneWeight(stones []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1000-1099/1046.Last Stone Weight/Solution.go b/solution/1000-1099/1046.Last Stone Weight/Solution.go index fdf6ac058e11a..d47ba67981cd8 100644 --- a/solution/1000-1099/1046.Last Stone Weight/Solution.go +++ b/solution/1000-1099/1046.Last Stone Weight/Solution.go @@ -15,9 +15,9 @@ func lastStoneWeight(stones []int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1000-1099/1063.Number of Valid Subarrays/Solution.go b/solution/1000-1099/1063.Number of Valid Subarrays/Solution.go index 3028b1ad72d04..d98e0a5a0db6c 100644 --- a/solution/1000-1099/1063.Number of Valid Subarrays/Solution.go +++ b/solution/1000-1099/1063.Number of Valid Subarrays/Solution.go @@ -1,17 +1,17 @@ -func validSubarrays(nums []int) (ans int) { - n := len(nums) - stk := []int{} - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && nums[stk[len(stk)-1]] >= nums[i] { - stk = stk[:len(stk)-1] - } - ans -= i - if len(stk) > 0 { - ans += stk[len(stk)-1] - } else { - ans += n - } - stk = append(stk, i) - } - return +func validSubarrays(nums []int) (ans int) { + n := len(nums) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && nums[stk[len(stk)-1]] >= nums[i] { + stk = stk[:len(stk)-1] + } + ans -= i + if len(stk) > 0 { + ans += stk[len(stk)-1] + } else { + ans += n + } + stk = append(stk, i) + } + return } \ No newline at end of file diff --git a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md index eac149c93242a..3c2cd91fb254f 100644 --- a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md +++ b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README.md @@ -147,9 +147,9 @@ func connectSticks(sticks []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README_EN.md b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README_EN.md index 4d12d983fbf11..bccf1715918c7 100644 --- a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README_EN.md +++ b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/README_EN.md @@ -131,9 +131,9 @@ func connectSticks(sticks []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.go b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.go index b94b01a515ffa..5b358ec054db2 100644 --- a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.go +++ b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.go @@ -11,9 +11,9 @@ func connectSticks(sticks []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1100-1199/1199.Minimum Time to Build Blocks/README.md b/solution/1100-1199/1199.Minimum Time to Build Blocks/README.md index 60cb4c97181e5..fdc03cc389f47 100644 --- a/solution/1100-1199/1199.Minimum Time to Build Blocks/README.md +++ b/solution/1100-1199/1199.Minimum Time to Build Blocks/README.md @@ -144,8 +144,8 @@ func minBuildTime(blocks []int, split int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1100-1199/1199.Minimum Time to Build Blocks/README_EN.md b/solution/1100-1199/1199.Minimum Time to Build Blocks/README_EN.md index 4144a06fe3022..96e23add4636c 100644 --- a/solution/1100-1199/1199.Minimum Time to Build Blocks/README_EN.md +++ b/solution/1100-1199/1199.Minimum Time to Build Blocks/README_EN.md @@ -120,8 +120,8 @@ func minBuildTime(blocks []int, split int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1100-1199/1199.Minimum Time to Build Blocks/Solution.go b/solution/1100-1199/1199.Minimum Time to Build Blocks/Solution.go index 406f9d3554c16..2ed613c01140f 100644 --- a/solution/1100-1199/1199.Minimum Time to Build Blocks/Solution.go +++ b/solution/1100-1199/1199.Minimum Time to Build Blocks/Solution.go @@ -12,8 +12,8 @@ func minBuildTime(blocks []int, split int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1200-1299/1231.Divide Chocolate/Solution.go b/solution/1200-1299/1231.Divide Chocolate/Solution.go index 78d0e7ea9e49a..1964d0f8a4825 100644 --- a/solution/1200-1299/1231.Divide Chocolate/Solution.go +++ b/solution/1200-1299/1231.Divide Chocolate/Solution.go @@ -1,26 +1,26 @@ -func maximizeSweetness(sweetness []int, k int) int { - l, r := 0, 0 - for _, v := range sweetness { - r += v - } - check := func(x int) bool { - s, cnt := 0, 0 - for _, v := range sweetness { - s += v - if s >= x { - s = 0 - cnt++ - } - } - return cnt > k - } - for l < r { - mid := (l + r + 1) >> 1 - if check(mid) { - l = mid - } else { - r = mid - 1 - } - } - return l +func maximizeSweetness(sweetness []int, k int) int { + l, r := 0, 0 + for _, v := range sweetness { + r += v + } + check := func(x int) bool { + s, cnt := 0, 0 + for _, v := range sweetness { + s += v + if s >= x { + s = 0 + cnt++ + } + } + return cnt > k + } + for l < r { + mid := (l + r + 1) >> 1 + if check(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l } \ No newline at end of file diff --git a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md index d4cfeecab53c0..8f0ca39d985d8 100644 --- a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md +++ b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md @@ -185,8 +185,8 @@ func maxEvents(events [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md index 09dcd955e9e58..e215357542f7d 100644 --- a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md +++ b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md @@ -163,8 +163,8 @@ func maxEvents(events [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/Solution.go b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/Solution.go index 7e4695a7ae874..df48690182d72 100644 --- a/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/Solution.go +++ b/solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/Solution.go @@ -26,8 +26,8 @@ func maxEvents(events [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1300-1399/1360.Number of Days Between Two Dates/Solution.go b/solution/1300-1399/1360.Number of Days Between Two Dates/Solution.go index 9069ad77698d6..2d8a33563d923 100644 --- a/solution/1300-1399/1360.Number of Days Between Two Dates/Solution.go +++ b/solution/1300-1399/1360.Number of Days Between Two Dates/Solution.go @@ -1,40 +1,40 @@ -func daysBetweenDates(date1 string, date2 string) int { - return abs(calcDays(date1) - calcDays(date2)) -} - -func isLeapYear(year int) bool { - return year%4 == 0 && (year%100 != 0 || year%400 == 0) -} - -func daysInMonth(year, month int) int { - days := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} - if isLeapYear(year) { - days[1] = 29 - } - return days[month-1] -} - -func calcDays(date string) int { - year, _ := strconv.Atoi(date[:4]) - month, _ := strconv.Atoi(date[5:7]) - day, _ := strconv.Atoi(date[8:]) - days := 0 - for y := 1971; y < year; y++ { - days += 365 - if isLeapYear(y) { - days++ - } - } - for m := 1; m < month; m++ { - days += daysInMonth(year, m) - } - days += day - return days -} - -func abs(x int) int { - if x < 0 { - return -x - } - return x +func daysBetweenDates(date1 string, date2 string) int { + return abs(calcDays(date1) - calcDays(date2)) +} + +func isLeapYear(year int) bool { + return year%4 == 0 && (year%100 != 0 || year%400 == 0) +} + +func daysInMonth(year, month int) int { + days := [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + if isLeapYear(year) { + days[1] = 29 + } + return days[month-1] +} + +func calcDays(date string) int { + year, _ := strconv.Atoi(date[:4]) + month, _ := strconv.Atoi(date[5:7]) + day, _ := strconv.Atoi(date[8:]) + days := 0 + for y := 1971; y < year; y++ { + days += 365 + if isLeapYear(y) { + days++ + } + } + for m := 1; m < month; m++ { + days += daysInMonth(year, m) + } + days += day + return days +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x } \ No newline at end of file diff --git a/solution/1300-1399/1363.Largest Multiple of Three/Solution.go b/solution/1300-1399/1363.Largest Multiple of Three/Solution.go index 96b6718ef3ea9..2a99ece8743d1 100644 --- a/solution/1300-1399/1363.Largest Multiple of Three/Solution.go +++ b/solution/1300-1399/1363.Largest Multiple of Three/Solution.go @@ -1,34 +1,34 @@ -func largestMultipleOfThree(digits []int) string { - sort.Ints(digits) - n := len(digits) - const inf = 1 << 30 - f := make([][]int, n+1) - for i := range f { - f[i] = make([]int, 3) - for j := range f[i] { - f[i][j] = -inf - } - } - f[0][0] = 0 - for i := 1; i <= n; i++ { - for j := 0; j < 3; j++ { - f[i][j] = max(f[i-1][j], f[i-1][(j-digits[i-1]%3+3)%3]+1) - } - } - if f[n][0] <= 0 { - return "" - } - ans := []byte{} - for i, j := n, 0; i > 0; i-- { - k := (j - digits[i-1]%3 + 3) % 3 - if f[i][j] == f[i-1][k]+1 { - ans = append(ans, byte('0'+digits[i-1])) - j = k - } - } - i := 0 - for i < len(ans)-1 && ans[i] == '0' { - i++ - } - return string(ans[i:]) +func largestMultipleOfThree(digits []int) string { + sort.Ints(digits) + n := len(digits) + const inf = 1 << 30 + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, 3) + for j := range f[i] { + f[i][j] = -inf + } + } + f[0][0] = 0 + for i := 1; i <= n; i++ { + for j := 0; j < 3; j++ { + f[i][j] = max(f[i-1][j], f[i-1][(j-digits[i-1]%3+3)%3]+1) + } + } + if f[n][0] <= 0 { + return "" + } + ans := []byte{} + for i, j := n, 0; i > 0; i-- { + k := (j - digits[i-1]%3 + 3) % 3 + if f[i][j] == f[i-1][k]+1 { + ans = append(ans, byte('0'+digits[i-1])) + j = k + } + } + i := 0 + for i < len(ans)-1 && ans[i] == '0' { + i++ + } + return string(ans[i:]) } \ No newline at end of file diff --git a/solution/1300-1399/1383.Maximum Performance of a Team/README.md b/solution/1300-1399/1383.Maximum Performance of a Team/README.md index 57e73036e218d..1e02c86718e03 100644 --- a/solution/1300-1399/1383.Maximum Performance of a Team/README.md +++ b/solution/1300-1399/1383.Maximum Performance of a Team/README.md @@ -173,8 +173,8 @@ func maxPerformance(n int, speed []int, efficiency []int, k int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1300-1399/1383.Maximum Performance of a Team/README_EN.md b/solution/1300-1399/1383.Maximum Performance of a Team/README_EN.md index d3bcac56722e0..0fa6a68523b9e 100644 --- a/solution/1300-1399/1383.Maximum Performance of a Team/README_EN.md +++ b/solution/1300-1399/1383.Maximum Performance of a Team/README_EN.md @@ -155,8 +155,8 @@ func maxPerformance(n int, speed []int, efficiency []int, k int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1300-1399/1383.Maximum Performance of a Team/Solution.go b/solution/1300-1399/1383.Maximum Performance of a Team/Solution.go index 486473d4fd97a..0baab57dab641 100644 --- a/solution/1300-1399/1383.Maximum Performance of a Team/Solution.go +++ b/solution/1300-1399/1383.Maximum Performance of a Team/Solution.go @@ -21,8 +21,8 @@ func maxPerformance(n int, speed []int, efficiency []int, k int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1400-1499/1405.Longest Happy String/README.md b/solution/1400-1499/1405.Longest Happy String/README.md index 98a8f0a7c9e0c..8727fba78ed9e 100644 --- a/solution/1400-1499/1405.Longest Happy String/README.md +++ b/solution/1400-1499/1405.Longest Happy String/README.md @@ -183,11 +183,11 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func longestDiverseString(a int, b int, c int) string { var h hp diff --git a/solution/1400-1499/1405.Longest Happy String/README_EN.md b/solution/1400-1499/1405.Longest Happy String/README_EN.md index d69ac19aec33f..47e8da597f4c1 100644 --- a/solution/1400-1499/1405.Longest Happy String/README_EN.md +++ b/solution/1400-1499/1405.Longest Happy String/README_EN.md @@ -170,11 +170,11 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func longestDiverseString(a int, b int, c int) string { var h hp diff --git a/solution/1400-1499/1405.Longest Happy String/Solution.go b/solution/1400-1499/1405.Longest Happy String/Solution.go index b562fe9f51934..342692947b4b2 100644 --- a/solution/1400-1499/1405.Longest Happy String/Solution.go +++ b/solution/1400-1499/1405.Longest Happy String/Solution.go @@ -5,11 +5,11 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].num > a[j].num } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func longestDiverseString(a int, b int, c int) string { var h hp diff --git a/solution/1400-1499/1449.Form Largest Integer With Digits That Add up to Target/Solution.go b/solution/1400-1499/1449.Form Largest Integer With Digits That Add up to Target/Solution.go index 2e0ae2a365e95..44598647dbdcd 100644 --- a/solution/1400-1499/1449.Form Largest Integer With Digits That Add up to Target/Solution.go +++ b/solution/1400-1499/1449.Form Largest Integer With Digits That Add up to Target/Solution.go @@ -1,38 +1,38 @@ -func largestNumber(cost []int, target int) string { - const inf = 1 << 30 - f := make([][]int, 10) - g := make([][]int, 10) - for i := range f { - f[i] = make([]int, target+1) - g[i] = make([]int, target+1) - for j := range f[i] { - f[i][j] = -inf - } - } - f[0][0] = 0 - for i := 1; i <= 9; i++ { - c := cost[i-1] - for j := 0; j <= target; j++ { - if j < c || f[i][j-c]+1 < f[i-1][j] { - f[i][j] = f[i-1][j] - g[i][j] = j - } else { - f[i][j] = f[i][j-c] + 1 - g[i][j] = j - c - } - } - } - if f[9][target] < 0 { - return "0" - } - ans := []byte{} - for i, j := 9, target; i > 0; { - if g[i][j] == j { - i-- - } else { - ans = append(ans, '0'+byte(i)) - j = g[i][j] - } - } - return string(ans) +func largestNumber(cost []int, target int) string { + const inf = 1 << 30 + f := make([][]int, 10) + g := make([][]int, 10) + for i := range f { + f[i] = make([]int, target+1) + g[i] = make([]int, target+1) + for j := range f[i] { + f[i][j] = -inf + } + } + f[0][0] = 0 + for i := 1; i <= 9; i++ { + c := cost[i-1] + for j := 0; j <= target; j++ { + if j < c || f[i][j-c]+1 < f[i-1][j] { + f[i][j] = f[i-1][j] + g[i][j] = j + } else { + f[i][j] = f[i][j-c] + 1 + g[i][j] = j - c + } + } + } + if f[9][target] < 0 { + return "0" + } + ans := []byte{} + for i, j := 9, target; i > 0; { + if g[i][j] == j { + i-- + } else { + ans = append(ans, '0'+byte(i)) + j = g[i][j] + } + } + return string(ans) } \ No newline at end of file diff --git a/solution/1400-1499/1467.Probability of a Two Boxes Having The Same Number of Distinct Balls/Solution.go b/solution/1400-1499/1467.Probability of a Two Boxes Having The Same Number of Distinct Balls/Solution.go index 2d9ab10c00b2d..0aaa381001d0b 100644 --- a/solution/1400-1499/1467.Probability of a Two Boxes Having The Same Number of Distinct Balls/Solution.go +++ b/solution/1400-1499/1467.Probability of a Two Boxes Having The Same Number of Distinct Balls/Solution.go @@ -1,60 +1,60 @@ -func getProbability(balls []int) float64 { - n, mx := 0, 0 - for _, x := range balls { - n += x - mx = max(mx, x) - } - n >>= 1 - m := max(mx, n<<1) - c := make([][]int, m+1) - for i := range c { - c[i] = make([]int, m+1) - } - for i := 0; i <= m; i++ { - c[i][0] = 1 - for j := 1; j <= i; j++ { - c[i][j] = c[i-1][j-1] + c[i-1][j] - } - } - k := len(balls) - f := make([][][]int, k) - for i := range f { - f[i] = make([][]int, n+1) - for j := range f[i] { - f[i][j] = make([]int, k<<1|1) - for h := range f[i][j] { - f[i][j][h] = -1 - } - } - } - var dfs func(int, int, int) int - dfs = func(i, j, diff int) int { - if i >= k { - if j == 0 && diff == k { - return 1 - } - return 0 - } - if j < 0 { - return 0 - } - if f[i][j][diff] != -1 { - return f[i][j][diff] - } - ans := 0 - for x := 0; x <= balls[i]; x++ { - y := 1 - if x != balls[i] { - if x == 0 { - y = -1 - } else { - y = 0 - } - } - ans += dfs(i+1, j-x, diff+y) * c[balls[i]][x] - } - f[i][j][diff] = ans - return ans - } - return float64(dfs(0, n, k)) / float64(c[n<<1][n]) +func getProbability(balls []int) float64 { + n, mx := 0, 0 + for _, x := range balls { + n += x + mx = max(mx, x) + } + n >>= 1 + m := max(mx, n<<1) + c := make([][]int, m+1) + for i := range c { + c[i] = make([]int, m+1) + } + for i := 0; i <= m; i++ { + c[i][0] = 1 + for j := 1; j <= i; j++ { + c[i][j] = c[i-1][j-1] + c[i-1][j] + } + } + k := len(balls) + f := make([][][]int, k) + for i := range f { + f[i] = make([][]int, n+1) + for j := range f[i] { + f[i][j] = make([]int, k<<1|1) + for h := range f[i][j] { + f[i][j][h] = -1 + } + } + } + var dfs func(int, int, int) int + dfs = func(i, j, diff int) int { + if i >= k { + if j == 0 && diff == k { + return 1 + } + return 0 + } + if j < 0 { + return 0 + } + if f[i][j][diff] != -1 { + return f[i][j][diff] + } + ans := 0 + for x := 0; x <= balls[i]; x++ { + y := 1 + if x != balls[i] { + if x == 0 { + y = -1 + } else { + y = 0 + } + } + ans += dfs(i+1, j-x, diff+y) * c[balls[i]][x] + } + f[i][j][diff] = ans + return ans + } + return float64(dfs(0, n, k)) / float64(c[n<<1][n]) } \ No newline at end of file diff --git a/solution/1400-1499/1499.Max Value of Equation/README.md b/solution/1400-1499/1499.Max Value of Equation/README.md index 617fe329d8f61..dc8789be5f672 100644 --- a/solution/1400-1499/1499.Max Value of Equation/README.md +++ b/solution/1400-1499/1499.Max Value of Equation/README.md @@ -235,9 +235,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/1400-1499/1499.Max Value of Equation/README_EN.md b/solution/1400-1499/1499.Max Value of Equation/README_EN.md index adaeddfd88ba9..93d2ade260f89 100644 --- a/solution/1400-1499/1499.Max Value of Equation/README_EN.md +++ b/solution/1400-1499/1499.Max Value of Equation/README_EN.md @@ -196,9 +196,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v > b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/1500-1599/1515.Best Position for a Service Centre/Solution.go b/solution/1500-1599/1515.Best Position for a Service Centre/Solution.go index 3bb9773bf688b..c6d50d4fd5119 100644 --- a/solution/1500-1599/1515.Best Position for a Service Centre/Solution.go +++ b/solution/1500-1599/1515.Best Position for a Service Centre/Solution.go @@ -1,33 +1,33 @@ -func getMinDistSum(positions [][]int) float64 { - n := len(positions) - var x, y float64 - for _, p := range positions { - x += float64(p[0]) - y += float64(p[1]) - } - x /= float64(n) - y /= float64(n) - const decay float64 = 0.999 - const eps float64 = 1e-6 - var alpha float64 = 0.5 - for { - var gradX, gradY float64 - var dist float64 - for _, p := range positions { - a := x - float64(p[0]) - b := y - float64(p[1]) - c := math.Sqrt(a*a + b*b) - gradX += a / (c + 1e-8) - gradY += b / (c + 1e-8) - dist += c - } - dx := gradX * alpha - dy := gradY * alpha - if math.Abs(dx) <= eps && math.Abs(dy) <= eps { - return dist - } - x -= dx - y -= dy - alpha *= decay - } +func getMinDistSum(positions [][]int) float64 { + n := len(positions) + var x, y float64 + for _, p := range positions { + x += float64(p[0]) + y += float64(p[1]) + } + x /= float64(n) + y /= float64(n) + const decay float64 = 0.999 + const eps float64 = 1e-6 + var alpha float64 = 0.5 + for { + var gradX, gradY float64 + var dist float64 + for _, p := range positions { + a := x - float64(p[0]) + b := y - float64(p[1]) + c := math.Sqrt(a*a + b*b) + gradX += a / (c + 1e-8) + gradY += b / (c + 1e-8) + dist += c + } + dx := gradX * alpha + dy := gradY * alpha + if math.Abs(dx) <= eps && math.Abs(dy) <= eps { + return dist + } + x -= dx + y -= dy + alpha *= decay + } } \ No newline at end of file diff --git a/solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/Solution.go b/solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/Solution.go index ffd953588dde3..935bf8678717a 100644 --- a/solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/Solution.go +++ b/solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/Solution.go @@ -1,22 +1,22 @@ -func closestToTarget(arr []int, target int) int { - ans := abs(arr[0] - target) - pre := map[int]bool{arr[0]: true} - for _, x := range arr { - cur := map[int]bool{x: true} - for y := range pre { - cur[x&y] = true - } - for y := range cur { - ans = min(ans, abs(y-target)) - } - pre = cur - } - return ans -} - -func abs(x int) int { - if x < 0 { - return -x - } - return x +func closestToTarget(arr []int, target int) int { + ans := abs(arr[0] - target) + pre := map[int]bool{arr[0]: true} + for _, x := range arr { + cur := map[int]bool{x: true} + for y := range pre { + cur[x&y] = true + } + for y := range cur { + ans = min(ans, abs(y-target)) + } + pre = cur + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x } \ No newline at end of file diff --git a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go index 113480adfd98a..7431f03478856 100644 --- a/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go +++ b/solution/1500-1599/1538.Guess the Majority in a Hidden Array/Solution.go @@ -1,56 +1,56 @@ -/** - * // This is the ArrayReader's API interface. - * // You should not implement it, or speculate about its implementation - * type ArrayReader struct { - * } - * // Compares 4 different elements in the array - * // return 4 if the values of the 4 elements are the same (0 or 1). - * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. - * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. - * func (this *ArrayReader) query(a, b, c, d int) int {} - * - * // Returns the length of the array - * func (this *ArrayReader) length() int {} - */ - -func guessMajority(reader *ArrayReader) int { - n := reader.length() - x := reader.query(0, 1, 2, 3) - a, b := 1, 0 - k := 0 - for i := 4; i < n; i++ { - if reader.query(0, 1, 2, i) == x { - a++ - } else { - b++ - k = i - } - } - - y := reader.query(0, 1, 2, 4) - if reader.query(1, 2, 3, 4) == y { - a++ - } else { - b++ - k = 0 - } - if reader.query(0, 2, 3, 4) == y { - a++ - } else { - b++ - k = 1 - } - if reader.query(0, 1, 3, 4) == y { - a++ - } else { - b++ - k = 2 - } - if a == b { - return -1 - } - if a > b { - return 3 - } - return k +/** + * // This is the ArrayReader's API interface. + * // You should not implement it, or speculate about its implementation + * type ArrayReader struct { + * } + * // Compares 4 different elements in the array + * // return 4 if the values of the 4 elements are the same (0 or 1). + * // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa. + * // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1. + * func (this *ArrayReader) query(a, b, c, d int) int {} + * + * // Returns the length of the array + * func (this *ArrayReader) length() int {} + */ + +func guessMajority(reader *ArrayReader) int { + n := reader.length() + x := reader.query(0, 1, 2, 3) + a, b := 1, 0 + k := 0 + for i := 4; i < n; i++ { + if reader.query(0, 1, 2, i) == x { + a++ + } else { + b++ + k = i + } + } + + y := reader.query(0, 1, 2, 4) + if reader.query(1, 2, 3, 4) == y { + a++ + } else { + b++ + k = 0 + } + if reader.query(0, 2, 3, 4) == y { + a++ + } else { + b++ + k = 1 + } + if reader.query(0, 1, 3, 4) == y { + a++ + } else { + b++ + k = 2 + } + if a == b { + return -1 + } + if a > b { + return 3 + } + return k } \ No newline at end of file diff --git a/solution/1500-1599/1548.The Most Similar Path in a Graph/Solution.go b/solution/1500-1599/1548.The Most Similar Path in a Graph/Solution.go index 1df124368a540..6532628c2c8e2 100644 --- a/solution/1500-1599/1548.The Most Similar Path in a Graph/Solution.go +++ b/solution/1500-1599/1548.The Most Similar Path in a Graph/Solution.go @@ -1,54 +1,54 @@ -func mostSimilar(n int, roads [][]int, names []string, targetPath []string) []int { - g := make([][]int, n) - for _, r := range roads { - a, b := r[0], r[1] - g[a] = append(g[a], b) - g[b] = append(g[b], a) - } - m := len(targetPath) - const inf = 1 << 30 - f := make([][]int, m) - pre := make([][]int, m) - for i := range f { - f[i] = make([]int, n) - pre[i] = make([]int, n) - for j := range f[i] { - f[i][j] = inf - pre[i][j] = -1 - } - } - for j, s := range names { - if targetPath[0] != s { - f[0][j] = 1 - } else { - f[0][j] = 0 - } - } - for i := 1; i < m; i++ { - for j := 0; j < n; j++ { - for _, k := range g[j] { - t := f[i-1][k] - if targetPath[i] != names[j] { - t++ - } - if t < f[i][j] { - f[i][j] = t - pre[i][j] = k - } - } - } - } - mi, k := inf, 0 - for j := 0; j < n; j++ { - if f[m-1][j] < mi { - mi = f[m-1][j] - k = j - } - } - ans := make([]int, m) - for i := m - 1; i >= 0; i-- { - ans[i] = k - k = pre[i][k] - } - return ans +func mostSimilar(n int, roads [][]int, names []string, targetPath []string) []int { + g := make([][]int, n) + for _, r := range roads { + a, b := r[0], r[1] + g[a] = append(g[a], b) + g[b] = append(g[b], a) + } + m := len(targetPath) + const inf = 1 << 30 + f := make([][]int, m) + pre := make([][]int, m) + for i := range f { + f[i] = make([]int, n) + pre[i] = make([]int, n) + for j := range f[i] { + f[i][j] = inf + pre[i][j] = -1 + } + } + for j, s := range names { + if targetPath[0] != s { + f[0][j] = 1 + } else { + f[0][j] = 0 + } + } + for i := 1; i < m; i++ { + for j := 0; j < n; j++ { + for _, k := range g[j] { + t := f[i-1][k] + if targetPath[i] != names[j] { + t++ + } + if t < f[i][j] { + f[i][j] = t + pre[i][j] = k + } + } + } + } + mi, k := inf, 0 + for j := 0; j < n; j++ { + if f[m-1][j] < mi { + mi = f[m-1][j] + k = j + } + } + ans := make([]int, m) + for i := m - 1; i >= 0; i-- { + ans[i] = k + k = pre[i][k] + } + return ans } \ No newline at end of file diff --git a/solution/1500-1599/1586.Binary Search Tree Iterator II/Solution.go b/solution/1500-1599/1586.Binary Search Tree Iterator II/Solution.go index 0adc899682b16..1db8eb953f20b 100644 --- a/solution/1500-1599/1586.Binary Search Tree Iterator II/Solution.go +++ b/solution/1500-1599/1586.Binary Search Tree Iterator II/Solution.go @@ -1,54 +1,54 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -type BSTIterator struct { - nums []int - i, n int -} - -func Constructor(root *TreeNode) BSTIterator { - nums := []int{} - var dfs func(*TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - dfs(root.Left) - nums = append(nums, root.Val) - dfs(root.Right) - } - dfs(root) - return BSTIterator{nums, -1, len(nums)} -} - -func (this *BSTIterator) HasNext() bool { - return this.i < this.n-1 -} - -func (this *BSTIterator) Next() int { - this.i++ - return this.nums[this.i] -} - -func (this *BSTIterator) HasPrev() bool { - return this.i > 0 -} - -func (this *BSTIterator) Prev() int { - this.i-- - return this.nums[this.i] -} - -/** - * Your BSTIterator object will be instantiated and called as such: - * obj := Constructor(root); - * param_1 := obj.HasNext(); - * param_2 := obj.Next(); - * param_3 := obj.HasPrev(); - * param_4 := obj.Prev(); +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +type BSTIterator struct { + nums []int + i, n int +} + +func Constructor(root *TreeNode) BSTIterator { + nums := []int{} + var dfs func(*TreeNode) + dfs = func(root *TreeNode) { + if root == nil { + return + } + dfs(root.Left) + nums = append(nums, root.Val) + dfs(root.Right) + } + dfs(root) + return BSTIterator{nums, -1, len(nums)} +} + +func (this *BSTIterator) HasNext() bool { + return this.i < this.n-1 +} + +func (this *BSTIterator) Next() int { + this.i++ + return this.nums[this.i] +} + +func (this *BSTIterator) HasPrev() bool { + return this.i > 0 +} + +func (this *BSTIterator) Prev() int { + this.i-- + return this.nums[this.i] +} + +/** + * Your BSTIterator object will be instantiated and called as such: + * obj := Constructor(root); + * param_1 := obj.HasNext(); + * param_2 := obj.Next(); + * param_3 := obj.HasPrev(); + * param_4 := obj.Prev(); */ \ No newline at end of file diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md index 06cabd6049667..e5f20997c431b 100644 --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README.md @@ -220,11 +220,11 @@ func busiestServers(k int, arrival, load []int) []int { type pair struct{ end, server int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md index e0196266e1d6e..77a1696867847 100644 --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/README_EN.md @@ -182,11 +182,11 @@ func busiestServers(k int, arrival, load []int) []int { type pair struct{ end, server int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.go b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.go index 78f09fd8c986a..36bb2d0a6fa45 100644 --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.go +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.go @@ -40,8 +40,8 @@ func busiestServers(k int, arrival, load []int) []int { type pair struct{ end, server int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].end < h[j].end } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/1600-1699/1642.Furthest Building You Can Reach/README.md b/solution/1600-1699/1642.Furthest Building You Can Reach/README.md index 3fa956d430544..ca03251ec2349 100644 --- a/solution/1600-1699/1642.Furthest Building You Can Reach/README.md +++ b/solution/1600-1699/1642.Furthest Building You Can Reach/README.md @@ -168,8 +168,8 @@ func furthestBuilding(heights []int, bricks int, ladders int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1600-1699/1642.Furthest Building You Can Reach/README_EN.md b/solution/1600-1699/1642.Furthest Building You Can Reach/README_EN.md index db1932ca828d2..d07f61bbf6d78 100644 --- a/solution/1600-1699/1642.Furthest Building You Can Reach/README_EN.md +++ b/solution/1600-1699/1642.Furthest Building You Can Reach/README_EN.md @@ -153,8 +153,8 @@ func furthestBuilding(heights []int, bricks int, ladders int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1600-1699/1642.Furthest Building You Can Reach/Solution.go b/solution/1600-1699/1642.Furthest Building You Can Reach/Solution.go index 5d017134928b7..439e53fd383c5 100644 --- a/solution/1600-1699/1642.Furthest Building You Can Reach/Solution.go +++ b/solution/1600-1699/1642.Furthest Building You Can Reach/Solution.go @@ -19,8 +19,8 @@ func furthestBuilding(heights []int, bricks int, ladders int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1600-1699/1675.Minimize Deviation in Array/README.md b/solution/1600-1699/1675.Minimize Deviation in Array/README.md index 9954f1e90eb49..6d7d78489ab08 100644 --- a/solution/1600-1699/1675.Minimize Deviation in Array/README.md +++ b/solution/1600-1699/1675.Minimize Deviation in Array/README.md @@ -183,8 +183,8 @@ func minimumDeviation(nums []int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md b/solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md index 60a7e0cba91b6..bd2f74e720f22 100644 --- a/solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md +++ b/solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md @@ -161,8 +161,8 @@ func minimumDeviation(nums []int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1600-1699/1675.Minimize Deviation in Array/Solution.go b/solution/1600-1699/1675.Minimize Deviation in Array/Solution.go index 1b55e51446274..9bfc18d763868 100644 --- a/solution/1600-1699/1675.Minimize Deviation in Array/Solution.go +++ b/solution/1600-1699/1675.Minimize Deviation in Array/Solution.go @@ -20,8 +20,8 @@ func minimumDeviation(nums []int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md index 6e5770a208555..ab678a114a627 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md @@ -176,11 +176,11 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **...** diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md index 71a323230988b..e91ef1c69c9e5 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md @@ -159,11 +159,11 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **...** diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.go b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.go index 08ab4c4753c9b..27dd7da925e84 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.go +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.go @@ -26,8 +26,8 @@ type pair struct { type hp []pair -func (a hp) Len() int { return len(a) } -func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file +func (a hp) Len() int { return len(a) } +func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a hp) Less(i, j int) bool { return a[i].first < a[j].first } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file diff --git a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README.md b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README.md index 291ac834ff529..03f8389b47144 100644 --- a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README.md +++ b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README.md @@ -307,9 +307,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func countRestrictedPaths(n int, edges [][]int) int { g := make([]pairs, n+1) diff --git a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README_EN.md b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README_EN.md index a2f3ae5404c64..764e7192ec100 100644 --- a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README_EN.md +++ b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/README_EN.md @@ -296,9 +296,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func countRestrictedPaths(n int, edges [][]int) int { g := make([]pairs, n+1) diff --git a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.go b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.go index a86c483823c31..ad0a3ec5483ae 100644 --- a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.go +++ b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.go @@ -14,9 +14,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } func countRestrictedPaths(n int, edges [][]int) int { g := make([]pairs, n+1) diff --git a/solution/1700-1799/1792.Maximum Average Pass Ratio/README.md b/solution/1700-1799/1792.Maximum Average Pass Ratio/README.md index 1d5e0fdc01c31..b81ea8d9b17ac 100644 --- a/solution/1700-1799/1792.Maximum Average Pass Ratio/README.md +++ b/solution/1700-1799/1792.Maximum Average Pass Ratio/README.md @@ -174,9 +174,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.x > b.x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md b/solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md index bdf07b08afa52..5af5e93dfcf50 100644 --- a/solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md +++ b/solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md @@ -152,9 +152,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.x > b.x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1700-1799/1792.Maximum Average Pass Ratio/Solution.go b/solution/1700-1799/1792.Maximum Average Pass Ratio/Solution.go index d7391116c644a..2cbcf523f0aa5 100644 --- a/solution/1700-1799/1792.Maximum Average Pass Ratio/Solution.go +++ b/solution/1700-1799/1792.Maximum Average Pass Ratio/Solution.go @@ -32,6 +32,6 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.x > b.x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/1800-1899/1801.Number of Orders in the Backlog/README.md b/solution/1800-1899/1801.Number of Orders in the Backlog/README.md index cacb106d2b056..c6a8697f3e0d9 100644 --- a/solution/1800-1899/1801.Number of Orders in the Backlog/README.md +++ b/solution/1800-1899/1801.Number of Orders in the Backlog/README.md @@ -274,11 +274,11 @@ func getNumberOfBacklogOrders(orders [][]int) (ans int) { type pair struct{ p, a int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md b/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md index beb566015d978..d348ea8fc343e 100644 --- a/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md +++ b/solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md @@ -257,11 +257,11 @@ func getNumberOfBacklogOrders(orders [][]int) (ans int) { type pair struct{ p, a int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1801.Number of Orders in the Backlog/Solution.go b/solution/1800-1899/1801.Number of Orders in the Backlog/Solution.go index e87dc3ad322e1..f0609a5d4960c 100644 --- a/solution/1800-1899/1801.Number of Orders in the Backlog/Solution.go +++ b/solution/1800-1899/1801.Number of Orders in the Backlog/Solution.go @@ -46,8 +46,8 @@ func getNumberOfBacklogOrders(orders [][]int) (ans int) { type pair struct{ p, a int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].p < h[j].p } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/1800-1899/1834.Single-Threaded CPU/README.md b/solution/1800-1899/1834.Single-Threaded CPU/README.md index 66655a804a507..0c09cd7c41a13 100644 --- a/solution/1800-1899/1834.Single-Threaded CPU/README.md +++ b/solution/1800-1899/1834.Single-Threaded CPU/README.md @@ -198,11 +198,11 @@ func getOrder(tasks [][]int) (ans []int) { type pair struct{ t, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1834.Single-Threaded CPU/README_EN.md b/solution/1800-1899/1834.Single-Threaded CPU/README_EN.md index 806ab499e76f3..2e058081dbd44 100644 --- a/solution/1800-1899/1834.Single-Threaded CPU/README_EN.md +++ b/solution/1800-1899/1834.Single-Threaded CPU/README_EN.md @@ -177,11 +177,11 @@ func getOrder(tasks [][]int) (ans []int) { type pair struct{ t, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1834.Single-Threaded CPU/Solution.go b/solution/1800-1899/1834.Single-Threaded CPU/Solution.go index e1c42abddcd6a..881c5a25f7467 100644 --- a/solution/1800-1899/1834.Single-Threaded CPU/Solution.go +++ b/solution/1800-1899/1834.Single-Threaded CPU/Solution.go @@ -23,8 +23,8 @@ func getOrder(tasks [][]int) (ans []int) { type pair struct{ t, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/1800-1899/1845.Seat Reservation Manager/README.md b/solution/1800-1899/1845.Seat Reservation Manager/README.md index 373bd4c6d0d11..727644656d8a8 100644 --- a/solution/1800-1899/1845.Seat Reservation Manager/README.md +++ b/solution/1800-1899/1845.Seat Reservation Manager/README.md @@ -180,9 +180,9 @@ func (this *SeatManager) Unreserve(seatNumber int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -209,13 +209,13 @@ public class SeatManager { availableSeats.Add(i); } } - + public int Reserve() { int reservedSeat = availableSeats.Min; availableSeats.Remove(reservedSeat); return reservedSeat; } - + public void Unreserve(int seatNumber) { availableSeats.Add(seatNumber); } diff --git a/solution/1800-1899/1845.Seat Reservation Manager/README_EN.md b/solution/1800-1899/1845.Seat Reservation Manager/README_EN.md index bc56a11388873..84ced81b35ea8 100644 --- a/solution/1800-1899/1845.Seat Reservation Manager/README_EN.md +++ b/solution/1800-1899/1845.Seat Reservation Manager/README_EN.md @@ -159,9 +159,9 @@ func (this *SeatManager) Unreserve(seatNumber int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -188,13 +188,13 @@ public class SeatManager { availableSeats.Add(i); } } - + public int Reserve() { int reservedSeat = availableSeats.Min; availableSeats.Remove(reservedSeat); return reservedSeat; } - + public void Unreserve(int seatNumber) { availableSeats.Add(seatNumber); } diff --git a/solution/1800-1899/1845.Seat Reservation Manager/Solution.go b/solution/1800-1899/1845.Seat Reservation Manager/Solution.go index 3e253b891a2b8..ce10256f0f003 100644 --- a/solution/1800-1899/1845.Seat Reservation Manager/Solution.go +++ b/solution/1800-1899/1845.Seat Reservation Manager/Solution.go @@ -20,9 +20,9 @@ func (this *SeatManager) Unreserve(seatNumber int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1800-1899/1847.Closest Room/Solution.go b/solution/1800-1899/1847.Closest Room/Solution.go index 704918b40ff45..1bc9056a0fe4f 100644 --- a/solution/1800-1899/1847.Closest Room/Solution.go +++ b/solution/1800-1899/1847.Closest Room/Solution.go @@ -1,48 +1,48 @@ -func closestRoom(rooms [][]int, queries [][]int) []int { - n, k := len(rooms), len(queries) - sort.Slice(rooms, func(i, j int) bool { return rooms[i][1] < rooms[j][1] }) - idx := make([]int, k) - ans := make([]int, k) - for i := range idx { - idx[i] = i - ans[i] = -1 - } - sort.Slice(idx, func(i, j int) bool { return queries[idx[i]][1] < queries[idx[j]][1] }) - rbt := redblacktree.NewWithIntComparator() - merge := func(rbt *redblacktree.Tree, key, value int) { - if v, ok := rbt.Get(key); ok { - nxt := v.(int) + value - if nxt == 0 { - rbt.Remove(key) - } else { - rbt.Put(key, nxt) - } - } else { - rbt.Put(key, value) - } - } - for _, room := range rooms { - merge(rbt, room[0], 1) - } - i := 0 - - for _, j := range idx { - prefer, minSize := queries[j][0], queries[j][1] - for i < n && rooms[i][1] < minSize { - merge(rbt, rooms[i][0], -1) - i++ - } - if i == n { - break - } - c, _ := rbt.Ceiling(prefer) - f, _ := rbt.Floor(prefer) - if c != nil { - ans[j] = c.Key.(int) - } - if f != nil && (ans[j] == -1 || ans[j]-prefer >= prefer-f.Key.(int)) { - ans[j] = f.Key.(int) - } - } - return ans +func closestRoom(rooms [][]int, queries [][]int) []int { + n, k := len(rooms), len(queries) + sort.Slice(rooms, func(i, j int) bool { return rooms[i][1] < rooms[j][1] }) + idx := make([]int, k) + ans := make([]int, k) + for i := range idx { + idx[i] = i + ans[i] = -1 + } + sort.Slice(idx, func(i, j int) bool { return queries[idx[i]][1] < queries[idx[j]][1] }) + rbt := redblacktree.NewWithIntComparator() + merge := func(rbt *redblacktree.Tree, key, value int) { + if v, ok := rbt.Get(key); ok { + nxt := v.(int) + value + if nxt == 0 { + rbt.Remove(key) + } else { + rbt.Put(key, nxt) + } + } else { + rbt.Put(key, value) + } + } + for _, room := range rooms { + merge(rbt, room[0], 1) + } + i := 0 + + for _, j := range idx { + prefer, minSize := queries[j][0], queries[j][1] + for i < n && rooms[i][1] < minSize { + merge(rbt, rooms[i][0], -1) + i++ + } + if i == n { + break + } + c, _ := rbt.Ceiling(prefer) + f, _ := rbt.Floor(prefer) + if c != nil { + ans[j] = c.Key.(int) + } + if f != nil && (ans[j] == -1 || ans[j]-prefer >= prefer-f.Key.(int)) { + ans[j] = f.Key.(int) + } + } + return ans } \ No newline at end of file diff --git a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md index 997027f30c5cf..b9c347f00d802 100644 --- a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md +++ b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md @@ -203,11 +203,11 @@ func minInterval(intervals [][]int, queries []int) []int { type pair struct{ v, r int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md index b2d125131c91e..ad94015619a2b 100644 --- a/solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md +++ b/solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md @@ -177,11 +177,11 @@ func minInterval(intervals [][]int, queries []int) []int { type pair struct{ v, r int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/1800-1899/1851.Minimum Interval to Include Each Query/Solution.go b/solution/1800-1899/1851.Minimum Interval to Include Each Query/Solution.go index 0fd961645563e..0d2e2083ff9d7 100644 --- a/solution/1800-1899/1851.Minimum Interval to Include Each Query/Solution.go +++ b/solution/1800-1899/1851.Minimum Interval to Include Each Query/Solution.go @@ -30,8 +30,8 @@ func minInterval(intervals [][]int, queries []int) []int { type pair struct{ v, r int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/1800-1899/1879.Minimum XOR Sum of Two Arrays/Solution.go b/solution/1800-1899/1879.Minimum XOR Sum of Two Arrays/Solution.go index c825f819f25fa..ca590d06d0b58 100644 --- a/solution/1800-1899/1879.Minimum XOR Sum of Two Arrays/Solution.go +++ b/solution/1800-1899/1879.Minimum XOR Sum of Two Arrays/Solution.go @@ -1,17 +1,17 @@ -func minimumXORSum(nums1 []int, nums2 []int) int { - n := len(nums1) - f := make([]int, 1<>j&1 == 1 { - f[i] = min(f[i], f[i^1<>j&1 == 1 { + f[i] = min(f[i], f[i^1< 0 { - f[i][j] = math.Min(f[i][j], f[i-1][j-1]+float64(dist[i-1])/float64(speed)) - } - } - } - for j := 0; j <= n; j++ { - if f[n][j] <= float64(hoursBefore) { - return j - } - } - return -1 +func minSkips(dist []int, speed int, hoursBefore int) int { + n := len(dist) + f := make([][]float64, n+1) + for i := range f { + f[i] = make([]float64, n+1) + for j := range f[i] { + f[i][j] = 1e20 + } + } + f[0][0] = 0 + eps := 1e-8 + for i := 1; i <= n; i++ { + for j := 0; j <= i; j++ { + if j < i { + f[i][j] = math.Min(f[i][j], math.Ceil(f[i-1][j]+float64(dist[i-1])/float64(speed)-eps)) + } + if j > 0 { + f[i][j] = math.Min(f[i][j], f[i-1][j-1]+float64(dist[i-1])/float64(speed)) + } + } + } + for j := 0; j <= n; j++ { + if f[n][j] <= float64(hoursBefore) { + return j + } + } + return -1 } \ No newline at end of file diff --git a/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.go b/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.go index def22346107fc..21103ea55d030 100644 --- a/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.go +++ b/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.go @@ -1,45 +1,45 @@ -func rotateGrid(grid [][]int, k int) [][]int { - m, n := len(grid), len(grid[0]) - - rotate := func(p, k int) { - nums := []int{} - for j := p; j < n-p-1; j++ { - nums = append(nums, grid[p][j]) - } - for i := p; i < m-p-1; i++ { - nums = append(nums, grid[i][n-p-1]) - } - for j := n - p - 1; j > p; j-- { - nums = append(nums, grid[m-p-1][j]) - } - for i := m - p - 1; i > p; i-- { - nums = append(nums, grid[i][p]) - } - l := len(nums) - k %= l - if k == 0 { - return - } - for j := p; j < n-p-1; j++ { - grid[p][j] = nums[k] - k = (k + 1) % l - } - for i := p; i < m-p-1; i++ { - grid[i][n-p-1] = nums[k] - k = (k + 1) % l - } - for j := n - p - 1; j > p; j-- { - grid[m-p-1][j] = nums[k] - k = (k + 1) % l - } - for i := m - p - 1; i > p; i-- { - grid[i][p] = nums[k] - k = (k + 1) % l - } - } - - for i := 0; i < m/2 && i < n/2; i++ { - rotate(i, k) - } - return grid +func rotateGrid(grid [][]int, k int) [][]int { + m, n := len(grid), len(grid[0]) + + rotate := func(p, k int) { + nums := []int{} + for j := p; j < n-p-1; j++ { + nums = append(nums, grid[p][j]) + } + for i := p; i < m-p-1; i++ { + nums = append(nums, grid[i][n-p-1]) + } + for j := n - p - 1; j > p; j-- { + nums = append(nums, grid[m-p-1][j]) + } + for i := m - p - 1; i > p; i-- { + nums = append(nums, grid[i][p]) + } + l := len(nums) + k %= l + if k == 0 { + return + } + for j := p; j < n-p-1; j++ { + grid[p][j] = nums[k] + k = (k + 1) % l + } + for i := p; i < m-p-1; i++ { + grid[i][n-p-1] = nums[k] + k = (k + 1) % l + } + for j := n - p - 1; j > p; j-- { + grid[m-p-1][j] = nums[k] + k = (k + 1) % l + } + for i := m - p - 1; i > p; i-- { + grid[i][p] = nums[k] + k = (k + 1) % l + } + } + + for i := 0; i < m/2 && i < n/2; i++ { + rotate(i, k) + } + return grid } \ No newline at end of file diff --git a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.go b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.go index f591b39c76d65..99d69024be683 100644 --- a/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.go +++ b/solution/1900-1999/1935.Maximum Number of Words You Can Type/Solution.go @@ -1,16 +1,16 @@ -func canBeTypedWords(text string, brokenLetters string) (ans int) { - s := [26]bool{} - for _, c := range brokenLetters { - s[c-'a'] = true - } - for _, w := range strings.Split(text, " ") { - for _, c := range w { - if s[c-'a'] { - ans-- - break - } - } - ans++ - } - return +func canBeTypedWords(text string, brokenLetters string) (ans int) { + s := [26]bool{} + for _, c := range brokenLetters { + s[c-'a'] = true + } + for _, w := range strings.Split(text, " ") { + for _, c := range w { + if s[c-'a'] { + ans-- + break + } + } + ans++ + } + return } \ No newline at end of file diff --git a/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.go b/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.go index fff606d656155..f79c3d88cc3fc 100644 --- a/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.go +++ b/solution/1900-1999/1936.Add Minimum Number of Rungs/Solution.go @@ -1,8 +1,8 @@ -func addRungs(rungs []int, dist int) (ans int) { - prev := 0 - for _, x := range rungs { - ans += (x - prev - 1) / dist - prev = x - } - return +func addRungs(rungs []int, dist int) (ans int) { + prev := 0 + for _, x := range rungs { + ans += (x - prev - 1) / dist + prev = x + } + return } \ No newline at end of file diff --git a/solution/1900-1999/1937.Maximum Number of Points with Cost/Solution.go b/solution/1900-1999/1937.Maximum Number of Points with Cost/Solution.go index 7ded2512cea05..65e9c87ca7152 100644 --- a/solution/1900-1999/1937.Maximum Number of Points with Cost/Solution.go +++ b/solution/1900-1999/1937.Maximum Number of Points with Cost/Solution.go @@ -1,22 +1,22 @@ -func maxPoints(points [][]int) (ans int64) { - n := len(points[0]) - const inf int64 = 1e18 - f := make([]int64, n) - for _, p := range points { - g := make([]int64, n) - lmx, rmx := -inf, -inf - for j := range p { - lmx = max(lmx, f[j]+int64(j)) - g[j] = max(g[j], int64(p[j])+lmx-int64(j)) - } - for j := n - 1; j >= 0; j-- { - rmx = max(rmx, f[j]-int64(j)) - g[j] = max(g[j], int64(p[j])+rmx+int64(j)) - } - f = g - } - for _, x := range f { - ans = max(ans, x) - } - return +func maxPoints(points [][]int) (ans int64) { + n := len(points[0]) + const inf int64 = 1e18 + f := make([]int64, n) + for _, p := range points { + g := make([]int64, n) + lmx, rmx := -inf, -inf + for j := range p { + lmx = max(lmx, f[j]+int64(j)) + g[j] = max(g[j], int64(p[j])+lmx-int64(j)) + } + for j := n - 1; j >= 0; j-- { + rmx = max(rmx, f[j]-int64(j)) + g[j] = max(g[j], int64(p[j])+rmx+int64(j)) + } + f = g + } + for _, x := range f { + ans = max(ans, x) + } + return } \ No newline at end of file diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.go b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.go index 78af69e285dc1..945a1c27771e7 100644 --- a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.go +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.go @@ -1,16 +1,16 @@ -func canSeePersonsCount(heights []int) []int { - n := len(heights) - ans := make([]int, n) - stk := []int{} - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && stk[len(stk)-1] < heights[i] { - ans[i]++ - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - ans[i]++ - } - stk = append(stk, heights[i]) - } - return ans +func canSeePersonsCount(heights []int) []int { + n := len(heights) + ans := make([]int, n) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && stk[len(stk)-1] < heights[i] { + ans[i]++ + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + ans[i]++ + } + stk = append(stk, heights[i]) + } + return ans } \ No newline at end of file diff --git a/solution/1900-1999/1955.Count Number of Special Subsequences/Solution.go b/solution/1900-1999/1955.Count Number of Special Subsequences/Solution.go index c4a1bc53904cd..a6496ea856f7f 100644 --- a/solution/1900-1999/1955.Count Number of Special Subsequences/Solution.go +++ b/solution/1900-1999/1955.Count Number of Special Subsequences/Solution.go @@ -1,18 +1,18 @@ -func countSpecialSubsequences(nums []int) int { - const mod = 1e9 + 7 - n := len(nums) - f := [3]int{} - if nums[0] == 0 { - f[0] = 1 - } - for i := 1; i < n; i++ { - if nums[i] == 0 { - f[0] = (2*f[0] + 1) % mod - } else if nums[i] == 1 { - f[1] = (f[0] + 2*f[1]) % mod - } else { - f[2] = (f[1] + 2*f[2]) % mod - } - } - return f[2] +func countSpecialSubsequences(nums []int) int { + const mod = 1e9 + 7 + n := len(nums) + f := [3]int{} + if nums[0] == 0 { + f[0] = 1 + } + for i := 1; i < n; i++ { + if nums[i] == 0 { + f[0] = (2*f[0] + 1) % mod + } else if nums[i] == 1 { + f[1] = (f[0] + 2*f[1]) % mod + } else { + f[2] = (f[1] + 2*f[2]) % mod + } + } + return f[2] } \ No newline at end of file diff --git a/solution/1900-1999/1962.Remove Stones to Minimize the Total/README.md b/solution/1900-1999/1962.Remove Stones to Minimize the Total/README.md index 7c6f4b0d77e2f..1e8817d75e9db 100644 --- a/solution/1900-1999/1962.Remove Stones to Minimize the Total/README.md +++ b/solution/1900-1999/1962.Remove Stones to Minimize the Total/README.md @@ -144,9 +144,9 @@ func minStoneSum(piles []int, k int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1900-1999/1962.Remove Stones to Minimize the Total/README_EN.md b/solution/1900-1999/1962.Remove Stones to Minimize the Total/README_EN.md index 8b45435fbea4a..b9b1037c7b5a5 100644 --- a/solution/1900-1999/1962.Remove Stones to Minimize the Total/README_EN.md +++ b/solution/1900-1999/1962.Remove Stones to Minimize the Total/README_EN.md @@ -132,9 +132,9 @@ func minStoneSum(piles []int, k int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.go b/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.go index 27c8a555c2710..a9caa8829fde3 100644 --- a/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.go +++ b/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.go @@ -15,9 +15,9 @@ func minStoneSum(piles []int, k int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/1900-1999/1986.Minimum Number of Work Sessions to Finish the Tasks/Solution.go b/solution/1900-1999/1986.Minimum Number of Work Sessions to Finish the Tasks/Solution.go index 834c0187f329f..97441fe91bf4d 100644 --- a/solution/1900-1999/1986.Minimum Number of Work Sessions to Finish the Tasks/Solution.go +++ b/solution/1900-1999/1986.Minimum Number of Work Sessions to Finish the Tasks/Solution.go @@ -1,23 +1,23 @@ -func minSessions(tasks []int, sessionTime int) int { - n := len(tasks) - ok := make([]bool, 1<>j&1 == 1 { - t += x - } - } - ok[i] = t <= sessionTime - } - for i := 1; i < 1< 0; j = (j - 1) & i { - if ok[j] { - f[i] = min(f[i], f[i^j]+1) - } - } - } - return f[1<>j&1 == 1 { + t += x + } + } + ok[i] = t <= sessionTime + } + for i := 1; i < 1< 0; j = (j - 1) & i { + if ok[j] { + f[i] = min(f[i], f[i^j]+1) + } + } + } + return f[1< 0 { - g[p] = append(g[p], i) - } - if nums[i] == 1 { - idx = i - } - ans[i] = 1 - } - if idx < 0 { - return ans - } - var dfs func(int) - dfs = func(i int) { - if vis[i] { - return - } - vis[i] = true - if nums[i] < len(has) { - has[nums[i]] = true - } - for _, j := range g[i] { - dfs(j) - } - } - for i := 2; idx != -1; idx = parents[idx] { - dfs(idx) - for has[i] { - i++ - } - ans[idx] = i - } - return ans +func smallestMissingValueSubtree(parents []int, nums []int) []int { + n := len(nums) + g := make([][]int, n) + vis := make([]bool, n) + has := make([]bool, n+2) + idx := -1 + ans := make([]int, n) + for i, p := range parents { + if i > 0 { + g[p] = append(g[p], i) + } + if nums[i] == 1 { + idx = i + } + ans[i] = 1 + } + if idx < 0 { + return ans + } + var dfs func(int) + dfs = func(i int) { + if vis[i] { + return + } + vis[i] = true + if nums[i] < len(has) { + has[nums[i]] = true + } + for _, j := range g[i] { + dfs(j) + } + } + for i := 2; idx != -1; idx = parents[idx] { + dfs(idx) + for has[i] { + i++ + } + ans[idx] = i + } + return ans } \ No newline at end of file diff --git a/solution/2000-2099/2019.The Score of Students Solving Math Expression/Solution.go b/solution/2000-2099/2019.The Score of Students Solving Math Expression/Solution.go index 557dcd42724ac..d29851c036f40 100644 --- a/solution/2000-2099/2019.The Score of Students Solving Math Expression/Solution.go +++ b/solution/2000-2099/2019.The Score of Students Solving Math Expression/Solution.go @@ -1,55 +1,55 @@ -func scoreOfStudents(s string, answers []int) int { - n := len(s) - x := cal(s) - m := (n + 1) >> 1 - f := make([][]map[int]bool, m) - for i := range f { - f[i] = make([]map[int]bool, m) - for j := range f[i] { - f[i][j] = make(map[int]bool) - } - f[i][i][int(s[i<<1]-'0')] = true - } - for i := m - 1; i >= 0; i-- { - for j := i; j < m; j++ { - for k := i; k < j; k++ { - for l := range f[i][k] { - for r := range f[k+1][j] { - op := s[k<<1|1] - if op == '+' && l+r <= 1000 { - f[i][j][l+r] = true - } else if op == '*' && l*r <= 1000 { - f[i][j][l*r] = true - } - } - } - } - } - } - cnt := [1001]int{} - for _, v := range answers { - cnt[v]++ - } - ans := cnt[x] * 5 - for k, v := range cnt { - if k != x && f[0][m-1][k] { - ans += v << 1 - } - } - return ans -} - -func cal(s string) int { - res, pre := 0, int(s[0]-'0') - for i := 1; i < len(s); i += 2 { - cur := int(s[i+1] - '0') - if s[i] == '+' { - res += pre - pre = cur - } else { - pre *= cur - } - } - res += pre - return res +func scoreOfStudents(s string, answers []int) int { + n := len(s) + x := cal(s) + m := (n + 1) >> 1 + f := make([][]map[int]bool, m) + for i := range f { + f[i] = make([]map[int]bool, m) + for j := range f[i] { + f[i][j] = make(map[int]bool) + } + f[i][i][int(s[i<<1]-'0')] = true + } + for i := m - 1; i >= 0; i-- { + for j := i; j < m; j++ { + for k := i; k < j; k++ { + for l := range f[i][k] { + for r := range f[k+1][j] { + op := s[k<<1|1] + if op == '+' && l+r <= 1000 { + f[i][j][l+r] = true + } else if op == '*' && l*r <= 1000 { + f[i][j][l*r] = true + } + } + } + } + } + } + cnt := [1001]int{} + for _, v := range answers { + cnt[v]++ + } + ans := cnt[x] * 5 + for k, v := range cnt { + if k != x && f[0][m-1][k] { + ans += v << 1 + } + } + return ans +} + +func cal(s string) int { + res, pre := 0, int(s[0]-'0') + for i := 1; i < len(s); i += 2 { + cur := int(s[i+1] - '0') + if s[i] == '+' { + res += pre + pre = cur + } else { + pre *= cur + } + } + res += pre + return res } \ No newline at end of file diff --git a/solution/2100-2199/2123.Minimum Operations to Remove Adjacent Ones in Matrix/Solution.go b/solution/2100-2199/2123.Minimum Operations to Remove Adjacent Ones in Matrix/Solution.go index 960f213e2b69f..fbb59692e1453 100644 --- a/solution/2100-2199/2123.Minimum Operations to Remove Adjacent Ones in Matrix/Solution.go +++ b/solution/2100-2199/2123.Minimum Operations to Remove Adjacent Ones in Matrix/Solution.go @@ -1,46 +1,46 @@ -func minimumOperations(grid [][]int) (ans int) { - m, n := len(grid), len(grid[0]) - vis := map[int]bool{} - match := make([]int, m*n) - for i := range match { - match[i] = -1 - } - g := map[int][]int{} - for i, row := range grid { - for j, v := range row { - if (i+j)&1 == 1 && v == 1 { - x := i*n + j - if i < m-1 && grid[i+1][j] == 1 { - g[x] = append(g[x], x+n) - } - if i > 0 && grid[i-1][j] == 1 { - g[x] = append(g[x], x-n) - } - if j < n-1 && grid[i][j+1] == 1 { - g[x] = append(g[x], x+1) - } - if j > 0 && grid[i][j-1] == 1 { - g[x] = append(g[x], x-1) - } - } - } - } - var find func(int) int - find = func(i int) int { - for _, j := range g[i] { - if !vis[j] { - vis[j] = true - if match[j] == -1 || find(match[j]) == 1 { - match[j] = i - return 1 - } - } - } - return 0 - } - for i := range g { - ans += find(i) - vis = map[int]bool{} - } - return +func minimumOperations(grid [][]int) (ans int) { + m, n := len(grid), len(grid[0]) + vis := map[int]bool{} + match := make([]int, m*n) + for i := range match { + match[i] = -1 + } + g := map[int][]int{} + for i, row := range grid { + for j, v := range row { + if (i+j)&1 == 1 && v == 1 { + x := i*n + j + if i < m-1 && grid[i+1][j] == 1 { + g[x] = append(g[x], x+n) + } + if i > 0 && grid[i-1][j] == 1 { + g[x] = append(g[x], x-n) + } + if j < n-1 && grid[i][j+1] == 1 { + g[x] = append(g[x], x+1) + } + if j > 0 && grid[i][j-1] == 1 { + g[x] = append(g[x], x-1) + } + } + } + } + var find func(int) int + find = func(i int) int { + for _, j := range g[i] { + if !vis[j] { + vis[j] = true + if match[j] == -1 || find(match[j]) == 1 { + match[j] = i + return 1 + } + } + } + return 0 + } + for i := range g { + ans += find(i) + vis = map[int]bool{} + } + return } \ No newline at end of file diff --git a/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.go b/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.go index bb348c4224e7d..85d6e64704d06 100644 --- a/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.go +++ b/solution/2100-2199/2141.Maximum Running Time of N Computers/Solution.go @@ -1,19 +1,19 @@ -func maxRunTime(n int, batteries []int) int64 { - l, r := 0, 0 - for _, x := range batteries { - r += x - } - for l < r { - mid := (l + r + 1) >> 1 - s := 0 - for _, x := range batteries { - s += min(x, mid) - } - if s >= n*mid { - l = mid - } else { - r = mid - 1 - } - } - return int64(l) +func maxRunTime(n int, batteries []int) int64 { + l, r := 0, 0 + for _, x := range batteries { + r += x + } + for l < r { + mid := (l + r + 1) >> 1 + s := 0 + for _, x := range batteries { + s += min(x, mid) + } + if s >= n*mid { + l = mid + } else { + r = mid - 1 + } + } + return int64(l) } \ No newline at end of file diff --git a/solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/Solution.go b/solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/Solution.go index 56c8f5b78848f..692f0bbdb9544 100644 --- a/solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/Solution.go +++ b/solution/2100-2199/2143.Choose Numbers From Two Arrays in Range/Solution.go @@ -1,33 +1,33 @@ -func countSubranges(nums1 []int, nums2 []int) (ans int) { - n := len(nums1) - s1, s2 := sum(nums1), sum(nums2) - f := make([][]int, n) - for i := range f { - f[i] = make([]int, s1+s2+1) - } - const mod int = 1e9 + 7 - for i, a := range nums1 { - b := nums2[i] - f[i][a+s2]++ - f[i][-b+s2]++ - if i > 0 { - for j := 0; j <= s1+s2; j++ { - if j >= a { - f[i][j] = (f[i][j] + f[i-1][j-a]) % mod - } - if j+b <= s1+s2 { - f[i][j] = (f[i][j] + f[i-1][j+b]) % mod - } - } - } - ans = (ans + f[i][s2]) % mod - } - return -} - -func sum(nums []int) (ans int) { - for _, x := range nums { - ans += x - } - return +func countSubranges(nums1 []int, nums2 []int) (ans int) { + n := len(nums1) + s1, s2 := sum(nums1), sum(nums2) + f := make([][]int, n) + for i := range f { + f[i] = make([]int, s1+s2+1) + } + const mod int = 1e9 + 7 + for i, a := range nums1 { + b := nums2[i] + f[i][a+s2]++ + f[i][-b+s2]++ + if i > 0 { + for j := 0; j <= s1+s2; j++ { + if j >= a { + f[i][j] = (f[i][j] + f[i-1][j-a]) % mod + } + if j+b <= s1+s2 { + f[i][j] = (f[i][j] + f[i-1][j+b]) % mod + } + } + } + ans = (ans + f[i][s2]) % mod + } + return +} + +func sum(nums []int) (ans int) { + for _, x := range nums { + ans += x + } + return } \ No newline at end of file diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md index 1bdf17fdd3fcb..f29a5a5a1de12 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md @@ -234,9 +234,9 @@ func minimumDifference(nums []int) int64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md index 3bffb63fc102e..b06ae00630045 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md @@ -216,9 +216,9 @@ func minimumDifference(nums []int) int64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.go b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.go index 5f5c2e4db2b62..b950f41fd5fef 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.go +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.go @@ -1,44 +1,44 @@ -func minimumDifference(nums []int) int64 { - m := len(nums) - n := m / 3 - s := 0 - pre := make([]int, m+1) - q1 := hp{} - for i := 1; i <= n*2; i++ { - x := nums[i-1] - s += x - heap.Push(&q1, -x) - if q1.Len() > n { - s -= -heap.Pop(&q1).(int) - } - pre[i] = s - } - s = 0 - suf := make([]int, m+1) - q2 := hp{} - for i := m; i > n; i-- { - x := nums[i-1] - s += x - heap.Push(&q2, x) - if q2.Len() > n { - s -= heap.Pop(&q2).(int) - } - suf[i] = s - } - ans := int64(1e18) - for i := n; i <= n*2; i++ { - ans = min(ans, int64(pre[i]-suf[i+1])) - } - return ans -} - -type hp struct{ sort.IntSlice } - -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v +func minimumDifference(nums []int) int64 { + m := len(nums) + n := m / 3 + s := 0 + pre := make([]int, m+1) + q1 := hp{} + for i := 1; i <= n*2; i++ { + x := nums[i-1] + s += x + heap.Push(&q1, -x) + if q1.Len() > n { + s -= -heap.Pop(&q1).(int) + } + pre[i] = s + } + s = 0 + suf := make([]int, m+1) + q2 := hp{} + for i := m; i > n; i-- { + x := nums[i-1] + s += x + heap.Push(&q2, x) + if q2.Len() > n { + s -= heap.Pop(&q2).(int) + } + suf[i] = s + } + ans := int64(1e18) + for i := n; i <= n*2; i++ { + ans = min(ans, int64(pre[i]-suf[i+1])) + } + return ans +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v } \ No newline at end of file diff --git a/solution/2100-2199/2172.Maximum AND Sum of Array/Solution.go b/solution/2100-2199/2172.Maximum AND Sum of Array/Solution.go index 8575394535840..e41b6f02f0c31 100644 --- a/solution/2100-2199/2172.Maximum AND Sum of Array/Solution.go +++ b/solution/2100-2199/2172.Maximum AND Sum of Array/Solution.go @@ -1,18 +1,18 @@ -func maximumANDSum(nums []int, numSlots int) (ans int) { - n := len(nums) - m := numSlots << 1 - f := make([]int, 1< n { - continue - } - for j := 0; j < m; j++ { - if i>>j&1 == 1 { - f[i] = max(f[i], f[i^(1< n { + continue + } + for j := 0; j < m; j++ { + if i>>j&1 == 1 { + f[i] = max(f[i], f[i^(1< h.Float64Slice[j] } -func (h *hp) Push(v interface{}) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] } +func (h *hp) Push(v any) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } +func (h *hp) Pop() any { a := h.Float64Slice v := a[len(a)-1] h.Float64Slice = a[:len(a)-1] @@ -191,8 +191,8 @@ func halveArray(nums []int) (ans int) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Push(interface{}) {} -func (hp) Pop() (_ interface{}) { return } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` ### **TypeScript** diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md index 337a6099377e5..ac7716c1802a9 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/README_EN.md @@ -143,9 +143,9 @@ func halveArray(nums []int) (ans int) { type hp struct{ sort.Float64Slice } -func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] } -func (h *hp) Push(v interface{}) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] } +func (h *hp) Push(v any) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } +func (h *hp) Pop() any { a := h.Float64Slice v := a[len(a)-1] h.Float64Slice = a[:len(a)-1] @@ -173,8 +173,8 @@ func halveArray(nums []int) (ans int) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Push(interface{}) {} -func (hp) Pop() (_ interface{}) { return } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` ### **TypeScript** diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go index d948eda08d4c1..e3d7dae5b4b2f 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.go @@ -17,9 +17,9 @@ func halveArray(nums []int) (ans int) { type hp struct{ sort.Float64Slice } -func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] } -func (h *hp) Push(v interface{}) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.Float64Slice[i] > h.Float64Slice[j] } +func (h *hp) Push(v any) { h.Float64Slice = append(h.Float64Slice, v.(float64)) } +func (h *hp) Pop() any { a := h.Float64Slice v := a[len(a)-1] h.Float64Slice = a[:len(a)-1] diff --git a/solution/2200-2299/2233.Maximum Product After K Increments/README.md b/solution/2200-2299/2233.Maximum Product After K Increments/README.md index f322cf37932c6..a8b70a70adf13 100644 --- a/solution/2200-2299/2233.Maximum Product After K Increments/README.md +++ b/solution/2200-2299/2233.Maximum Product After K Increments/README.md @@ -133,8 +133,8 @@ func maximumProduct(nums []int, k int) int { type hp struct{ sort.IntSlice } -func (hp) Push(interface{}) {} -func (hp) Pop() (_ interface{}) { return } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` ### **JavaScript** diff --git a/solution/2200-2299/2233.Maximum Product After K Increments/README_EN.md b/solution/2200-2299/2233.Maximum Product After K Increments/README_EN.md index b550394376797..0eb6a710d2efe 100644 --- a/solution/2200-2299/2233.Maximum Product After K Increments/README_EN.md +++ b/solution/2200-2299/2233.Maximum Product After K Increments/README_EN.md @@ -119,8 +119,8 @@ func maximumProduct(nums []int, k int) int { type hp struct{ sort.IntSlice } -func (hp) Push(interface{}) {} -func (hp) Pop() (_ interface{}) { return } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` ### **JavaScript** diff --git a/solution/2200-2299/2233.Maximum Product After K Increments/Solution.go b/solution/2200-2299/2233.Maximum Product After K Increments/Solution.go index 55f217a4dadba..916e86c9c3ce5 100644 --- a/solution/2200-2299/2233.Maximum Product After K Increments/Solution.go +++ b/solution/2200-2299/2233.Maximum Product After K Increments/Solution.go @@ -13,5 +13,5 @@ func maximumProduct(nums []int, k int) int { type hp struct{ sort.IntSlice } -func (hp) Push(interface{}) {} -func (hp) Pop() (_ interface{}) { return } \ No newline at end of file +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } \ No newline at end of file diff --git a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/Solution.go b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/Solution.go index d7e8d8b7c2f89..5a80ba4ac3b0a 100644 --- a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/Solution.go +++ b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/Solution.go @@ -1,22 +1,22 @@ -func longestPath(parent []int, s string) int { - n := len(parent) - g := make([][]int, n) - for i := 1; i < n; i++ { - g[parent[i]] = append(g[parent[i]], i) - } - ans := 0 - var dfs func(int) int - dfs = func(i int) int { - mx := 0 - for _, j := range g[i] { - x := dfs(j) + 1 - if s[i] != s[j] { - ans = max(ans, x+mx) - mx = max(mx, x) - } - } - return mx - } - dfs(0) - return ans + 1 +func longestPath(parent []int, s string) int { + n := len(parent) + g := make([][]int, n) + for i := 1; i < n; i++ { + g[parent[i]] = append(g[parent[i]], i) + } + ans := 0 + var dfs func(int) int + dfs = func(i int) int { + mx := 0 + for _, j := range g[i] { + x := dfs(j) + 1 + if s[i] != s[j] { + ans = max(ans, x+mx) + mx = max(mx, x) + } + } + return mx + } + dfs(0) + return ans + 1 } \ No newline at end of file diff --git a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/Solution.go b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/Solution.go index 38ba0060b2d62..9f8c45dfb54ce 100644 --- a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/Solution.go +++ b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/Solution.go @@ -1,38 +1,38 @@ -func maximumCost(n int, highways [][]int, k int) int { - if k >= n { - return -1 - } - g := make([][][2]int, n) - for _, h := range highways { - a, b, cost := h[0], h[1], h[2] - g[a] = append(g[a], [2]int{b, cost}) - g[b] = append(g[b], [2]int{a, cost}) - } - f := make([][]int, 1<>j&1 == 1 { - for _, e := range g[j] { - h, cost := e[0], e[1] - if i>>h&1 == 1 { - f[i][j] = max(f[i][j], f[i^(1<= n { + return -1 + } + g := make([][][2]int, n) + for _, h := range highways { + a, b, cost := h[0], h[1], h[2] + g[a] = append(g[a], [2]int{b, cost}) + g[b] = append(g[b], [2]int{a, cost}) + } + f := make([][]int, 1<>j&1 == 1 { + for _, e := range g[j] { + h, cost := e[0], e[1] + if i>>h&1 == 1 { + f[i][j] = max(f[i][j], f[i^(1<= 0; i-- { - for len(stk) > 0 && stk[len(stk)-1] < nums[i] { - ans[i]++ - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - ans[i]++ - } - for len(stk) > 0 && stk[len(stk)-1] == nums[i] { - stk = stk[:len(stk)-1] - } - stk = append(stk, nums[i]) - } - return ans - } - for _, row := range heights { - ans = append(ans, f(row)) - } - n := len(heights[0]) - for j := 0; j < n; j++ { - col := make([]int, len(heights)) - for i := range heights { - col[i] = heights[i][j] - } - for i, v := range f(col) { - ans[i][j] += v - } - } - return +func seePeople(heights [][]int) (ans [][]int) { + f := func(nums []int) []int { + n := len(nums) + ans := make([]int, n) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && stk[len(stk)-1] < nums[i] { + ans[i]++ + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + ans[i]++ + } + for len(stk) > 0 && stk[len(stk)-1] == nums[i] { + stk = stk[:len(stk)-1] + } + stk = append(stk, nums[i]) + } + return ans + } + for _, row := range heights { + ans = append(ans, f(row)) + } + n := len(heights[0]) + for j := 0; j < n; j++ { + col := make([]int, len(heights)) + for i := range heights { + col[i] = heights[i][j] + } + for i, v := range f(col) { + ans[i][j] += v + } + } + return } \ No newline at end of file diff --git a/solution/2300-2399/2313.Minimum Flips in Binary Tree to Get Result/Solution.go b/solution/2300-2399/2313.Minimum Flips in Binary Tree to Get Result/Solution.go index 52f5bf96b1474..08acde9211dc2 100644 --- a/solution/2300-2399/2313.Minimum Flips in Binary Tree to Get Result/Solution.go +++ b/solution/2300-2399/2313.Minimum Flips in Binary Tree to Get Result/Solution.go @@ -1,42 +1,42 @@ -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -func minimumFlips(root *TreeNode, result bool) int { - var dfs func(*TreeNode) (int, int) - dfs = func(root *TreeNode) (int, int) { - if root == nil { - return 1 << 30, 1 << 30 - } - x := root.Val - if x < 2 { - return x, x ^ 1 - } - l0, l1 := dfs(root.Left) - r0, r1 := dfs(root.Right) - var a, b int - if x == 2 { - a = l0 + r0 - b = min(l0+r1, min(l1+r0, l1+r1)) - } else if x == 3 { - a = min(l0+r0, min(l0+r1, l1+r0)) - b = l1 + r1 - } else if x == 4 { - a = min(l0+r0, l1+r1) - b = min(l0+r1, l1+r0) - } else { - a = min(l1, r1) - b = min(l0, r0) - } - return a, b - } - a, b := dfs(root) - if result { - return b - } - return a +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func minimumFlips(root *TreeNode, result bool) int { + var dfs func(*TreeNode) (int, int) + dfs = func(root *TreeNode) (int, int) { + if root == nil { + return 1 << 30, 1 << 30 + } + x := root.Val + if x < 2 { + return x, x ^ 1 + } + l0, l1 := dfs(root.Left) + r0, r1 := dfs(root.Right) + var a, b int + if x == 2 { + a = l0 + r0 + b = min(l0+r1, min(l1+r0, l1+r1)) + } else if x == 3 { + a = min(l0+r0, min(l0+r1, l1+r0)) + b = l1 + r1 + } else if x == 4 { + a = min(l0+r0, l1+r1) + b = min(l0+r1, l1+r0) + } else { + a = min(l1, r1) + b = min(l0, r0) + } + return a, b + } + a, b := dfs(root) + if result { + return b + } + return a } \ No newline at end of file diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md b/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md index fa77d0d5ad975..c24254fd2d2b1 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/README.md @@ -303,14 +303,14 @@ func (this *SmallestInfiniteSet) AddBack(num int) { type hp []int -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i] < h[j] } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(int)) } -func (h *hp) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return } -func (h *hp) push(v int) { heap.Push(h, v) } -func (h *hp) pop() int { return heap.Pop(h).(int) } -func (h *hp) top() int { a := *h; return a[0] } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i] < h[j] } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(int)) } +func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return } +func (h *hp) push(v int) { heap.Push(h, v) } +func (h *hp) pop() int { return heap.Pop(h).(int) } +func (h *hp) top() int { a := *h; return a[0] } /** * Your SmallestInfiniteSet object will be instantiated and called as such: diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md b/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md index 6f226943c2679..2c7d7608d47ed 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md @@ -291,14 +291,14 @@ func (this *SmallestInfiniteSet) AddBack(num int) { type hp []int -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i] < h[j] } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(int)) } -func (h *hp) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return } -func (h *hp) push(v int) { heap.Push(h, v) } -func (h *hp) pop() int { return heap.Pop(h).(int) } -func (h *hp) top() int { a := *h; return a[0] } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i] < h[j] } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(int)) } +func (h *hp) Pop() (v any) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return } +func (h *hp) push(v int) { heap.Push(h, v) } +func (h *hp) pop() int { return heap.Pop(h).(int) } +func (h *hp) top() int { a := *h; return a[0] } /** * Your SmallestInfiniteSet object will be instantiated and called as such: diff --git a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README.md b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README.md index 2371216d662b5..df2fdb7f82132 100644 --- a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README.md +++ b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README.md @@ -369,11 +369,11 @@ func closestMeetingNode(edges []int, node1 int, node2 int) int { type pair struct{ d, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README_EN.md b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README_EN.md index 6b5054a40b5b8..a85a7c1d2b1bd 100644 --- a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README_EN.md +++ b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README_EN.md @@ -349,11 +349,11 @@ func closestMeetingNode(edges []int, node1 int, node2 int) int { type pair struct{ d, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/Solution.go b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/Solution.go index 01e25b587c0da..052996ec8a859 100644 --- a/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/Solution.go +++ b/solution/2300-2399/2359.Find Closest Node to Given Two Nodes/Solution.go @@ -43,8 +43,8 @@ func closestMeetingNode(edges []int, node1 int, node2 int) int { type pair struct{ d, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2300-2399/2361.Minimum Costs Using the Train Line/Solution.go b/solution/2300-2399/2361.Minimum Costs Using the Train Line/Solution.go index 49ee0c5e838af..114916f9a6319 100644 --- a/solution/2300-2399/2361.Minimum Costs Using the Train Line/Solution.go +++ b/solution/2300-2399/2361.Minimum Costs Using the Train Line/Solution.go @@ -1,12 +1,12 @@ -func minimumCosts(regular []int, express []int, expressCost int) []int64 { - f, g := 0, 1<<30 - cost := make([]int64, len(regular)) - for i, a := range regular { - b := express[i] - ff := min(f+a, g+a) - gg := min(f+expressCost+b, g+b) - f, g = ff, gg - cost[i] = int64(min(f, g)) - } - return cost +func minimumCosts(regular []int, express []int, expressCost int) []int64 { + f, g := 0, 1<<30 + cost := make([]int64, len(regular)) + for i, a := range regular { + b := express[i] + ff := min(f+a, g+a) + gg := min(f+expressCost+b, g+b) + f, g = ff, gg + cost[i] = int64(min(f, g)) + } + return cost } \ No newline at end of file diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/README.md b/solution/2300-2399/2386.Find the K-Sum of an Array/README.md index 8de8d30621000..4d9b4270df16d 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/README.md +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/README.md @@ -192,11 +192,11 @@ func kSum(nums []int, k int) int64 { type pair struct{ sum, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md b/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md index c6ccae8caab41..3dc3608c5e2c3 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md @@ -170,11 +170,11 @@ func kSum(nums []int, k int) int64 { type pair struct{ sum, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go index 86ff30b9e04c6..fadb5bf736d73 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go @@ -25,8 +25,8 @@ func kSum(nums []int, k int) int64 { type pair struct{ sum, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2400-2499/2402.Meeting Rooms III/README.md b/solution/2400-2499/2402.Meeting Rooms III/README.md index 3dbe832f392c8..b32e6029a4294 100644 --- a/solution/2400-2499/2402.Meeting Rooms III/README.md +++ b/solution/2400-2499/2402.Meeting Rooms III/README.md @@ -239,8 +239,8 @@ func mostBooked(n int, meetings [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -255,9 +255,9 @@ func (h hp2) Less(i, j int) bool { a, b := h[i], h[j] return a.end < b.end || a.end == b.end && a.i < b.i } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2402.Meeting Rooms III/README_EN.md b/solution/2400-2499/2402.Meeting Rooms III/README_EN.md index dbd5b3a7942b2..e261f43f2c56d 100644 --- a/solution/2400-2499/2402.Meeting Rooms III/README_EN.md +++ b/solution/2400-2499/2402.Meeting Rooms III/README_EN.md @@ -217,8 +217,8 @@ func mostBooked(n int, meetings [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -233,9 +233,9 @@ func (h hp2) Less(i, j int) bool { a, b := h[i], h[j] return a.end < b.end || a.end == b.end && a.i < b.i } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2402.Meeting Rooms III/Solution.go b/solution/2400-2499/2402.Meeting Rooms III/Solution.go index 29a6905601fd5..7d0dceadae8c1 100644 --- a/solution/2400-2499/2402.Meeting Rooms III/Solution.go +++ b/solution/2400-2499/2402.Meeting Rooms III/Solution.go @@ -33,8 +33,8 @@ func mostBooked(n int, meetings [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -49,6 +49,6 @@ func (h hp2) Less(i, j int) bool { a, b := h[i], h[j] return a.end < b.end || a.end == b.end && a.i < b.i } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README.md b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README.md index 4fae72d28a863..e1cf506deb7ce 100644 --- a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README.md +++ b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README.md @@ -131,8 +131,8 @@ func minGroups(intervals [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README_EN.md b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README_EN.md index 3b7ab99da0c54..8b60069b974fb 100644 --- a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README_EN.md +++ b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/README_EN.md @@ -113,8 +113,8 @@ func minGroups(intervals [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/Solution.go b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/Solution.go index 00a9243feb99b..51f0954f859ca 100644 --- a/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/Solution.go +++ b/solution/2400-2499/2406.Divide Intervals Into Minimum Number of Groups/Solution.go @@ -12,8 +12,8 @@ func minGroups(intervals [][]int) int { type hp struct{ sort.IntSlice } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2400-2499/2454.Next Greater Element IV/README.md b/solution/2400-2499/2454.Next Greater Element IV/README.md index e7a7ee837ecd3..05da24c904279 100644 --- a/solution/2400-2499/2454.Next Greater Element IV/README.md +++ b/solution/2400-2499/2454.Next Greater Element IV/README.md @@ -185,9 +185,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v < b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2454.Next Greater Element IV/README_EN.md b/solution/2400-2499/2454.Next Greater Element IV/README_EN.md index 67896c04ef113..a7d5aae768b33 100644 --- a/solution/2400-2499/2454.Next Greater Element IV/README_EN.md +++ b/solution/2400-2499/2454.Next Greater Element IV/README_EN.md @@ -165,9 +165,9 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v < b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2454.Next Greater Element IV/Solution.go b/solution/2400-2499/2454.Next Greater Element IV/Solution.go index 8a0fc9d9976ba..dd1aeaa772cb8 100644 --- a/solution/2400-2499/2454.Next Greater Element IV/Solution.go +++ b/solution/2400-2499/2454.Next Greater Element IV/Solution.go @@ -29,6 +29,6 @@ func (h hp) Less(i, j int) bool { a, b := h[i], h[j] return a.v < b.v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2400-2499/2462.Total Cost to Hire K Workers/README.md b/solution/2400-2499/2462.Total Cost to Hire K Workers/README.md index 817e0adabad7c..57db33e154e93 100644 --- a/solution/2400-2499/2462.Total Cost to Hire K Workers/README.md +++ b/solution/2400-2499/2462.Total Cost to Hire K Workers/README.md @@ -226,11 +226,11 @@ func totalCost(costs []int, k int, candidates int) int64 { type pair struct{ c, x int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2462.Total Cost to Hire K Workers/README_EN.md b/solution/2400-2499/2462.Total Cost to Hire K Workers/README_EN.md index b27149e710353..ad4ae87a8aa3b 100644 --- a/solution/2400-2499/2462.Total Cost to Hire K Workers/README_EN.md +++ b/solution/2400-2499/2462.Total Cost to Hire K Workers/README_EN.md @@ -206,11 +206,11 @@ func totalCost(costs []int, k int, candidates int) int64 { type pair struct{ c, x int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2400-2499/2462.Total Cost to Hire K Workers/Solution.go b/solution/2400-2499/2462.Total Cost to Hire K Workers/Solution.go index 8d0ffc3b4be42..bfd669b6fd333 100644 --- a/solution/2400-2499/2462.Total Cost to Hire K Workers/Solution.go +++ b/solution/2400-2499/2462.Total Cost to Hire K Workers/Solution.go @@ -35,8 +35,8 @@ func totalCost(costs []int, k int, candidates int) int64 { type pair struct{ c, x int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].c < h[j].c || h[i].c == h[j].c && h[i].x < h[j].x } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2400-2499/2473.Minimum Cost to Buy Apples/README.md b/solution/2400-2499/2473.Minimum Cost to Buy Apples/README.md index 11987914e7e22..01e51402a1974 100644 --- a/solution/2400-2499/2473.Minimum Cost to Buy Apples/README.md +++ b/solution/2400-2499/2473.Minimum Cost to Buy Apples/README.md @@ -253,9 +253,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **TypeScript** diff --git a/solution/2400-2499/2473.Minimum Cost to Buy Apples/README_EN.md b/solution/2400-2499/2473.Minimum Cost to Buy Apples/README_EN.md index 07e02301b69f3..951562330990d 100644 --- a/solution/2400-2499/2473.Minimum Cost to Buy Apples/README_EN.md +++ b/solution/2400-2499/2473.Minimum Cost to Buy Apples/README_EN.md @@ -234,9 +234,9 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **TypeScript** diff --git a/solution/2400-2499/2473.Minimum Cost to Buy Apples/Solution.go b/solution/2400-2499/2473.Minimum Cost to Buy Apples/Solution.go index f6a5b94321fb5..d8d5443c00e55 100644 --- a/solution/2400-2499/2473.Minimum Cost to Buy Apples/Solution.go +++ b/solution/2400-2499/2473.Minimum Cost to Buy Apples/Solution.go @@ -47,6 +47,6 @@ func (a pairs) Len() int { return len(a) } func (a pairs) Less(i int, j int) bool { return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second } -func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } -func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file +func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] } +func (a *pairs) Push(x any) { *a = append(*a, x.(pair)) } +func (a *pairs) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go index 6588ed4a222d1..bd7f270e88234 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution.go @@ -34,9 +34,9 @@ func countGreatEnoughNodes(root *TreeNode, k int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README.md b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README.md index 48d48dc6d704f..fd7451a073735 100644 --- a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README.md +++ b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README.md @@ -256,11 +256,11 @@ type pair struct{ v, i int } type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README_EN.md b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README_EN.md index 88e7af85b32a1..38e899536875b 100644 --- a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README_EN.md +++ b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/README_EN.md @@ -234,11 +234,11 @@ type pair struct{ v, i int } type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/Solution.go b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/Solution.go index 3c340a07576c6..2c4c5973fc487 100644 --- a/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/Solution.go +++ b/solution/2500-2599/2503.Maximum Number of Points From Grid Queries/Solution.go @@ -37,8 +37,8 @@ type pair struct{ v, i int } type tuple struct{ v, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2500-2599/2532.Time to Cross a Bridge/README.md b/solution/2500-2599/2532.Time to Cross a Bridge/README.md index f677ddd94c825..9d90fa4e182f7 100644 --- a/solution/2500-2599/2532.Time to Cross a Bridge/README.md +++ b/solution/2500-2599/2532.Time to Cross a Bridge/README.md @@ -358,9 +358,9 @@ func findCrossingTime(n int, k int, time [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -370,11 +370,11 @@ func (h *hp) Pop() interface{} { type pair struct{ t, i int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md b/solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md index bc3c847d0ad2a..ae0654300db95 100644 --- a/solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md +++ b/solution/2500-2599/2532.Time to Cross a Bridge/README_EN.md @@ -323,9 +323,9 @@ func findCrossingTime(n int, k int, time [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -335,11 +335,11 @@ func (h *hp) Pop() interface{} { type pair struct{ t, i int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2532.Time to Cross a Bridge/Solution.go b/solution/2500-2599/2532.Time to Cross a Bridge/Solution.go index 03d71f12161f1..5ebdcd68db8ad 100644 --- a/solution/2500-2599/2532.Time to Cross a Bridge/Solution.go +++ b/solution/2500-2599/2532.Time to Cross a Bridge/Solution.go @@ -52,9 +52,9 @@ func findCrossingTime(n int, k int, time [][]int) int { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] @@ -64,8 +64,8 @@ func (h *hp) Pop() interface{} { type pair struct{ t, i int } type hp2 []pair -func (h hp2) Len() int { return len(h) } -func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp2) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp2) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp2) Len() int { return len(h) } +func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2500-2599/2542.Maximum Subsequence Score/README.md b/solution/2500-2599/2542.Maximum Subsequence Score/README.md index 94ad778464572..54abdcc2cc875 100644 --- a/solution/2500-2599/2542.Maximum Subsequence Score/README.md +++ b/solution/2500-2599/2542.Maximum Subsequence Score/README.md @@ -169,9 +169,9 @@ func maxScore(nums1 []int, nums2 []int, k int) int64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2542.Maximum Subsequence Score/README_EN.md b/solution/2500-2599/2542.Maximum Subsequence Score/README_EN.md index de5733c501c18..05f467c56c4c7 100644 --- a/solution/2500-2599/2542.Maximum Subsequence Score/README_EN.md +++ b/solution/2500-2599/2542.Maximum Subsequence Score/README_EN.md @@ -153,9 +153,9 @@ func maxScore(nums1 []int, nums2 []int, k int) int64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2542.Maximum Subsequence Score/Solution.go b/solution/2500-2599/2542.Maximum Subsequence Score/Solution.go index 7c1c2c1fea53d..ea16e3983539e 100644 --- a/solution/2500-2599/2542.Maximum Subsequence Score/Solution.go +++ b/solution/2500-2599/2542.Maximum Subsequence Score/Solution.go @@ -22,9 +22,9 @@ func maxScore(nums1 []int, nums2 []int, k int) int64 { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README.md b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README.md index f0cea8b3306e4..11e8c01b18db1 100644 --- a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README.md +++ b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README.md @@ -231,11 +231,11 @@ func minimumTime(grid [][]int) int { type tuple struct{ t, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md index 4f382cda3dd6f..266ea02f16cd3 100644 --- a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md +++ b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/README_EN.md @@ -218,11 +218,11 @@ func minimumTime(grid [][]int) int { type tuple struct{ t, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **...** diff --git a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/Solution.go b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/Solution.go index 83a59de018046..909b8467c3f61 100644 --- a/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/Solution.go +++ b/solution/2500-2599/2577.Minimum Time to Visit a Cell In a Grid/Solution.go @@ -39,8 +39,8 @@ func minimumTime(grid [][]int) int { type tuple struct{ t, i, j int } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].t < h[j].t } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README.md b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README.md index 6409a21c62421..33f1f7137236a 100644 --- a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README.md +++ b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README.md @@ -261,11 +261,11 @@ func findScore(nums []int) (ans int64) { type pair struct{ x, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md index 2b2eab4554a8c..fc644d88664df 100644 --- a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md +++ b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md @@ -253,11 +253,11 @@ func findScore(nums []int) (ans int64) { type pair struct{ x, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ```go diff --git a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/Solution.go b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/Solution.go index a9ab53fa3c1c6..bb7c9201dbfbc 100644 --- a/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/Solution.go +++ b/solution/2500-2599/2593.Find Score of an Array After Marking All Elements/Solution.go @@ -25,8 +25,8 @@ func findScore(nums []int) (ans int64) { type pair struct{ x, i int } type hp []pair -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].x < h[j].x || (h[i].x == h[j].x && h[i].i < h[j].i) } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(pair)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README.md b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README.md index 6b3a5ad3ffe34..451604d0a5427 100644 --- a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README.md +++ b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README.md @@ -149,9 +149,9 @@ func makePrefSumNonNegative(nums []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README_EN.md b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README_EN.md index bb571f44bffa3..6ab1ffadb4983 100644 --- a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README_EN.md +++ b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/README_EN.md @@ -139,9 +139,9 @@ func makePrefSumNonNegative(nums []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/Solution.go b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/Solution.go index 230c5a0abc1b6..665f871d7eecf 100644 --- a/solution/2500-2599/2599.Make the Prefix Sum Non-negative/Solution.go +++ b/solution/2500-2599/2599.Make the Prefix Sum Non-negative/Solution.go @@ -16,9 +16,9 @@ func makePrefSumNonNegative(nums []int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README.md b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README.md index 494b40caa3119..97bf4b18d21ab 100644 --- a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README.md +++ b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README.md @@ -238,8 +238,8 @@ func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a hp) Less(i, j int) bool { return a[i].first < a[j].first || (a[i].first == a[j].first && a[i].second < a[j].second) } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **...** diff --git a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README_EN.md b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README_EN.md index c6fd449122c5a..d2c5c68091a77 100644 --- a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README_EN.md +++ b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/README_EN.md @@ -225,8 +225,8 @@ func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a hp) Less(i, j int) bool { return a[i].first < a[j].first || (a[i].first == a[j].first && a[i].second < a[j].second) } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } ``` ### **...** diff --git a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/Solution.go b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/Solution.go index 40eba42e4da00..190e1268da86c 100644 --- a/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/Solution.go +++ b/solution/2600-2699/2617.Minimum Number of Visited Cells in a Grid/Solution.go @@ -45,5 +45,5 @@ func (a hp) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a hp) Less(i, j int) bool { return a[i].first < a[j].first || (a[i].first == a[j].first && a[i].second < a[j].second) } -func (a *hp) Push(x interface{}) { *a = append(*a, x.(pair)) } -func (a *hp) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file +func (a *hp) Push(x any) { *a = append(*a, x.(pair)) } +func (a *hp) Pop() any { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t } \ No newline at end of file diff --git a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README.md b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README.md index 4e7691ce748dd..f848690d88e13 100644 --- a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README.md +++ b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README.md @@ -207,11 +207,11 @@ type tuple struct { } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README_EN.md b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README_EN.md index fe5964317615f..c724d66fdd2a9 100644 --- a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README_EN.md +++ b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/README_EN.md @@ -199,11 +199,11 @@ type tuple struct { } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` ### **TypeScript** diff --git a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/Solution.go b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/Solution.go index cc627fc379af4..f70aa26496366 100644 --- a/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/Solution.go +++ b/solution/2600-2699/2662.Minimum Cost of a Path With Special Roads/Solution.go @@ -36,8 +36,8 @@ type tuple struct { } type hp []tuple -func (h hp) Len() int { return len(h) } -func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } -func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *hp) Push(v interface{}) { *h = append(*h, v.(tuple)) } -func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file +func (h hp) Len() int { return len(h) } +func (h hp) Less(i, j int) bool { return h[i].d < h[j].d } +func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } +func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } \ No newline at end of file diff --git a/solution/2700-2799/2731.Movement of Robots/Solution.go b/solution/2700-2799/2731.Movement of Robots/Solution.go index ad0f83a5f52ab..4bdd269ac0c9a 100644 --- a/solution/2700-2799/2731.Movement of Robots/Solution.go +++ b/solution/2700-2799/2731.Movement of Robots/Solution.go @@ -1,17 +1,17 @@ -func sumDistance(nums []int, s string, d int) (ans int) { - for i, c := range s { - if c == 'R' { - nums[i] += d - } else { - nums[i] -= d - } - } - sort.Ints(nums) - sum := 0 - const mod int = 1e9 + 7 - for i, x := range nums { - ans = (ans + i*x - sum) % mod - sum += x - } - return +func sumDistance(nums []int, s string, d int) (ans int) { + for i, c := range s { + if c == 'R' { + nums[i] += d + } else { + nums[i] -= d + } + } + sort.Ints(nums) + sum := 0 + const mod int = 1e9 + 7 + for i, x := range nums { + ans = (ans + i*x - sum) % mod + sum += x + } + return } \ No newline at end of file diff --git a/solution/2700-2799/2792.Count Nodes That Are Great Enough/README.md b/solution/2700-2799/2792.Count Nodes That Are Great Enough/README.md index 38f3a07a2a62c..76da7185dc29e 100644 --- a/solution/2700-2799/2792.Count Nodes That Are Great Enough/README.md +++ b/solution/2700-2799/2792.Count Nodes That Are Great Enough/README.md @@ -258,9 +258,9 @@ func countGreatEnoughNodes(root *TreeNode, k int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2700-2799/2792.Count Nodes That Are Great Enough/README_EN.md b/solution/2700-2799/2792.Count Nodes That Are Great Enough/README_EN.md index c1c270355778b..257ae7bb6af21 100644 --- a/solution/2700-2799/2792.Count Nodes That Are Great Enough/README_EN.md +++ b/solution/2700-2799/2792.Count Nodes That Are Great Enough/README_EN.md @@ -239,9 +239,9 @@ func countGreatEnoughNodes(root *TreeNode, k int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2700-2799/2792.Count Nodes That Are Great Enough/Solution.go b/solution/2700-2799/2792.Count Nodes That Are Great Enough/Solution.go index 6588ed4a222d1..bd7f270e88234 100644 --- a/solution/2700-2799/2792.Count Nodes That Are Great Enough/Solution.go +++ b/solution/2700-2799/2792.Count Nodes That Are Great Enough/Solution.go @@ -34,9 +34,9 @@ func countGreatEnoughNodes(root *TreeNode, k int) (ans int) { type hp struct{ sort.IntSlice } -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() interface{} { +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { a := h.IntSlice v := a[len(a)-1] h.IntSlice = a[:len(a)-1] diff --git a/solution/2800-2899/2808.Minimum Seconds to Equalize a Circular Array/Solution.go b/solution/2800-2899/2808.Minimum Seconds to Equalize a Circular Array/Solution.go index cc51df4b762bc..f780f6631e5ef 100644 --- a/solution/2800-2899/2808.Minimum Seconds to Equalize a Circular Array/Solution.go +++ b/solution/2800-2899/2808.Minimum Seconds to Equalize a Circular Array/Solution.go @@ -1,17 +1,17 @@ -func minimumSeconds(nums []int) int { - d := map[int][]int{} - for i, x := range nums { - d[x] = append(d[x], i) - } - ans := 1 << 30 - n := len(nums) - for _, idx := range d { - m := len(idx) - t := idx[0] + n - idx[m-1] - for i := 1; i < m; i++ { - t = max(t, idx[i]-idx[i-1]) - } - ans = min(ans, t/2) - } - return ans +func minimumSeconds(nums []int) int { + d := map[int][]int{} + for i, x := range nums { + d[x] = append(d[x], i) + } + ans := 1 << 30 + n := len(nums) + for _, idx := range d { + m := len(idx) + t := idx[0] + n - idx[m-1] + for i := 1; i < m; i++ { + t = max(t, idx[i]-idx[i-1]) + } + ans = min(ans, t/2) + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2809.Minimum Time to Make Array Sum At Most x/Solution.go b/solution/2800-2899/2809.Minimum Time to Make Array Sum At Most x/Solution.go index 4ee5c5a2b9661..082eadd2ec107 100644 --- a/solution/2800-2899/2809.Minimum Time to Make Array Sum At Most x/Solution.go +++ b/solution/2800-2899/2809.Minimum Time to Make Array Sum At Most x/Solution.go @@ -1,25 +1,25 @@ -func minimumTime(nums1 []int, nums2 []int, x int) int { - n := len(nums1) - f := make([]int, n+1) - type pair struct{ a, b int } - nums := make([]pair, n) - var s1, s2 int - for i := range nums { - s1 += nums1[i] - s2 += nums2[i] - nums[i] = pair{nums1[i], nums2[i]} - } - sort.Slice(nums, func(i, j int) bool { return nums[i].b < nums[j].b }) - for _, e := range nums { - a, b := e.a, e.b - for j := n; j > 0; j-- { - f[j] = max(f[j], f[j-1]+a+b*j) - } - } - for j := 0; j <= n; j++ { - if s1+s2*j-f[j] <= x { - return j - } - } - return -1 +func minimumTime(nums1 []int, nums2 []int, x int) int { + n := len(nums1) + f := make([]int, n+1) + type pair struct{ a, b int } + nums := make([]pair, n) + var s1, s2 int + for i := range nums { + s1 += nums1[i] + s2 += nums2[i] + nums[i] = pair{nums1[i], nums2[i]} + } + sort.Slice(nums, func(i, j int) bool { return nums[i].b < nums[j].b }) + for _, e := range nums { + a, b := e.a, e.b + for j := n; j > 0; j-- { + f[j] = max(f[j], f[j-1]+a+b*j) + } + } + for j := 0; j <= n; j++ { + if s1+s2*j-f[j] <= x { + return j + } + } + return -1 } \ No newline at end of file diff --git a/solution/2800-2899/2810.Faulty Keyboard/Solution.go b/solution/2800-2899/2810.Faulty Keyboard/Solution.go index baefbc7fda120..4092ceb76c1cb 100644 --- a/solution/2800-2899/2810.Faulty Keyboard/Solution.go +++ b/solution/2800-2899/2810.Faulty Keyboard/Solution.go @@ -1,13 +1,13 @@ -func finalString(s string) string { - t := []rune{} - for _, c := range s { - if c == 'i' { - for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 { - t[i], t[j] = t[j], t[i] - } - } else { - t = append(t, c) - } - } - return string(t) +func finalString(s string) string { + t := []rune{} + for _, c := range s { + if c == 'i' { + for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 { + t[i], t[j] = t[j], t[i] + } + } else { + t = append(t, c) + } + } + return string(t) } \ No newline at end of file diff --git a/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.go b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.go index fa8ddae53d0d6..b7cb77d22136f 100644 --- a/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.go +++ b/solution/2800-2899/2811.Check if it is Possible to Split Array/Solution.go @@ -1,31 +1,31 @@ -func canSplitArray(nums []int, m int) bool { - n := len(nums) - f := make([][]int, n) - s := make([]int, n+1) - for i, x := range nums { - s[i+1] = s[i] + x - } - for i := range f { - f[i] = make([]int, n) - } - var dfs func(i, j int) bool - dfs = func(i, j int) bool { - if i == j { - return true - } - if f[i][j] != 0 { - return f[i][j] == 1 - } - for k := i; k < j; k++ { - a := k == i || s[k+1]-s[i] >= m - b := k == j-1 || s[j+1]-s[k+1] >= m - if a && b && dfs(i, k) && dfs(k+1, j) { - f[i][j] = 1 - return true - } - } - f[i][j] = -1 - return false - } - return dfs(0, n-1) +func canSplitArray(nums []int, m int) bool { + n := len(nums) + f := make([][]int, n) + s := make([]int, n+1) + for i, x := range nums { + s[i+1] = s[i] + x + } + for i := range f { + f[i] = make([]int, n) + } + var dfs func(i, j int) bool + dfs = func(i, j int) bool { + if i == j { + return true + } + if f[i][j] != 0 { + return f[i][j] == 1 + } + for k := i; k < j; k++ { + a := k == i || s[k+1]-s[i] >= m + b := k == j-1 || s[j+1]-s[k+1] >= m + if a && b && dfs(i, k) && dfs(k+1, j) { + f[i][j] = 1 + return true + } + } + f[i][j] = -1 + return false + } + return dfs(0, n-1) } \ No newline at end of file diff --git a/solution/2800-2899/2813.Maximum Elegance of a K-Length Subsequence/Solution.go b/solution/2800-2899/2813.Maximum Elegance of a K-Length Subsequence/Solution.go index 762df51d24984..ad51ede4fc760 100644 --- a/solution/2800-2899/2813.Maximum Elegance of a K-Length Subsequence/Solution.go +++ b/solution/2800-2899/2813.Maximum Elegance of a K-Length Subsequence/Solution.go @@ -1,27 +1,27 @@ -func findMaximumElegance(items [][]int, k int) int64 { - sort.Slice(items, func(i, j int) bool { return items[i][0] > items[j][0] }) - tot := 0 - vis := map[int]bool{} - dup := []int{} - for _, item := range items[:k] { - p, c := item[0], item[1] - tot += p - if vis[c] { - dup = append(dup, p) - } else { - vis[c] = true - } - } - ans := tot + len(vis)*len(vis) - for _, item := range items[k:] { - p, c := item[0], item[1] - if vis[c] || len(dup) == 0 { - continue - } - vis[c] = true - tot += p - dup[len(dup)-1] - dup = dup[:len(dup)-1] - ans = max(ans, tot+len(vis)*len(vis)) - } - return int64(ans) +func findMaximumElegance(items [][]int, k int) int64 { + sort.Slice(items, func(i, j int) bool { return items[i][0] > items[j][0] }) + tot := 0 + vis := map[int]bool{} + dup := []int{} + for _, item := range items[:k] { + p, c := item[0], item[1] + tot += p + if vis[c] { + dup = append(dup, p) + } else { + vis[c] = true + } + } + ans := tot + len(vis)*len(vis) + for _, item := range items[k:] { + p, c := item[0], item[1] + if vis[c] || len(dup) == 0 { + continue + } + vis[c] = true + tot += p - dup[len(dup)-1] + dup = dup[:len(dup)-1] + ans = max(ans, tot+len(vis)*len(vis)) + } + return int64(ans) } \ No newline at end of file diff --git a/solution/2800-2899/2815.Max Pair Sum in an Array/Solution.go b/solution/2800-2899/2815.Max Pair Sum in an Array/Solution.go index 3ac4da3003ac4..a77efed92d348 100644 --- a/solution/2800-2899/2815.Max Pair Sum in an Array/Solution.go +++ b/solution/2800-2899/2815.Max Pair Sum in an Array/Solution.go @@ -1,18 +1,18 @@ -func maxSum(nums []int) int { - ans := -1 - f := func(x int) int { - y := 0 - for ; x > 0; x /= 10 { - y = max(y, x%10) - } - return y - } - for i, x := range nums { - for _, y := range nums[i+1:] { - if v := x + y; ans < v && f(x) == f(y) { - ans = v - } - } - } - return ans +func maxSum(nums []int) int { + ans := -1 + f := func(x int) int { + y := 0 + for ; x > 0; x /= 10 { + y = max(y, x%10) + } + return y + } + for i, x := range nums { + for _, y := range nums[i+1:] { + if v := x + y; ans < v && f(x) == f(y) { + ans = v + } + } + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2816.Double a Number Represented as a Linked List/Solution.go b/solution/2800-2899/2816.Double a Number Represented as a Linked List/Solution.go index adba72d4fc55a..a974c08a0c056 100644 --- a/solution/2800-2899/2816.Double a Number Represented as a Linked List/Solution.go +++ b/solution/2800-2899/2816.Double a Number Represented as a Linked List/Solution.go @@ -1,36 +1,36 @@ -/** - * Definition for singly-linked list. - * type ListNode struct { - * Val int - * Next *ListNode - * } - */ -func doubleIt(head *ListNode) *ListNode { - head = reverse(head) - dummy := &ListNode{} - cur := dummy - mul, carry := 2, 0 - for head != nil { - x := head.Val*mul + carry - carry = x / 10 - cur.Next = &ListNode{Val: x % 10} - cur = cur.Next - head = head.Next - } - if carry > 0 { - cur.Next = &ListNode{Val: carry} - } - return reverse(dummy.Next) -} - -func reverse(head *ListNode) *ListNode { - dummy := &ListNode{} - cur := head - for cur != nil { - next := cur.Next - cur.Next = dummy.Next - dummy.Next = cur - cur = next - } - return dummy.Next +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func doubleIt(head *ListNode) *ListNode { + head = reverse(head) + dummy := &ListNode{} + cur := dummy + mul, carry := 2, 0 + for head != nil { + x := head.Val*mul + carry + carry = x / 10 + cur.Next = &ListNode{Val: x % 10} + cur = cur.Next + head = head.Next + } + if carry > 0 { + cur.Next = &ListNode{Val: carry} + } + return reverse(dummy.Next) +} + +func reverse(head *ListNode) *ListNode { + dummy := &ListNode{} + cur := head + for cur != nil { + next := cur.Next + cur.Next = dummy.Next + dummy.Next = cur + cur = next + } + return dummy.Next } \ No newline at end of file diff --git a/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/Solution.go b/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/Solution.go index 7ea45d70b6034..7248725617294 100644 --- a/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/Solution.go +++ b/solution/2800-2899/2817.Minimum Absolute Difference Between Elements With Constraint/Solution.go @@ -1,16 +1,16 @@ -func minAbsoluteDifference(nums []int, x int) int { - rbt := redblacktree.NewWithIntComparator() - ans := 1 << 30 - for i := x; i < len(nums); i++ { - rbt.Put(nums[i-x], nil) - c, _ := rbt.Ceiling(nums[i]) - f, _ := rbt.Floor(nums[i]) - if c != nil { - ans = min(ans, c.Key.(int)-nums[i]) - } - if f != nil { - ans = min(ans, nums[i]-f.Key.(int)) - } - } - return ans +func minAbsoluteDifference(nums []int, x int) int { + rbt := redblacktree.NewWithIntComparator() + ans := 1 << 30 + for i := x; i < len(nums); i++ { + rbt.Put(nums[i-x], nil) + c, _ := rbt.Ceiling(nums[i]) + f, _ := rbt.Floor(nums[i]) + if c != nil { + ans = min(ans, c.Key.(int)-nums[i]) + } + if f != nil { + ans = min(ans, nums[i]-f.Key.(int)) + } + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2818.Apply Operations to Maximize Score/Solution.go b/solution/2800-2899/2818.Apply Operations to Maximize Score/Solution.go index 43e76c17382e7..229e74a96b051 100644 --- a/solution/2800-2899/2818.Apply Operations to Maximize Score/Solution.go +++ b/solution/2800-2899/2818.Apply Operations to Maximize Score/Solution.go @@ -1,75 +1,75 @@ -func maximumScore(nums []int, k int) int { - n := len(nums) - const mod = 1e9 + 7 - qpow := func(a, n int) int { - ans := 1 - for ; n > 0; n >>= 1 { - if n&1 == 1 { - ans = ans * a % mod - } - a = a * a % mod - } - return ans - } - arr := make([][3]int, n) - left := make([]int, n) - right := make([]int, n) - for i, x := range nums { - left[i] = -1 - right[i] = n - arr[i] = [3]int{i, primeFactors(x), x} - } - stk := []int{} - for _, e := range arr { - i, f := e[0], e[1] - for len(stk) > 0 && arr[stk[len(stk)-1]][1] < f { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - left[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - stk = []int{} - for i := n - 1; i >= 0; i-- { - f := arr[i][1] - for len(stk) > 0 && arr[stk[len(stk)-1]][1] <= f { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - right[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - sort.Slice(arr, func(i, j int) bool { return arr[i][2] > arr[j][2] }) - ans := 1 - for _, e := range arr { - i, x := e[0], e[2] - l, r := left[i], right[i] - cnt := (i - l) * (r - i) - if cnt <= k { - ans = ans * qpow(x, cnt) % mod - k -= cnt - } else { - ans = ans * qpow(x, k) % mod - break - } - } - return ans -} - -func primeFactors(n int) int { - i := 2 - ans := map[int]bool{} - for i <= n/i { - for n%i == 0 { - ans[i] = true - n /= i - } - i++ - } - if n > 1 { - ans[n] = true - } - return len(ans) +func maximumScore(nums []int, k int) int { + n := len(nums) + const mod = 1e9 + 7 + qpow := func(a, n int) int { + ans := 1 + for ; n > 0; n >>= 1 { + if n&1 == 1 { + ans = ans * a % mod + } + a = a * a % mod + } + return ans + } + arr := make([][3]int, n) + left := make([]int, n) + right := make([]int, n) + for i, x := range nums { + left[i] = -1 + right[i] = n + arr[i] = [3]int{i, primeFactors(x), x} + } + stk := []int{} + for _, e := range arr { + i, f := e[0], e[1] + for len(stk) > 0 && arr[stk[len(stk)-1]][1] < f { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + left[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + stk = []int{} + for i := n - 1; i >= 0; i-- { + f := arr[i][1] + for len(stk) > 0 && arr[stk[len(stk)-1]][1] <= f { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + right[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + sort.Slice(arr, func(i, j int) bool { return arr[i][2] > arr[j][2] }) + ans := 1 + for _, e := range arr { + i, x := e[0], e[2] + l, r := left[i], right[i] + cnt := (i - l) * (r - i) + if cnt <= k { + ans = ans * qpow(x, cnt) % mod + k -= cnt + } else { + ans = ans * qpow(x, k) % mod + break + } + } + return ans +} + +func primeFactors(n int) int { + i := 2 + ans := map[int]bool{} + for i <= n/i { + for n%i == 0 { + ans[i] = true + n /= i + } + i++ + } + if n > 1 { + ans[n] = true + } + return len(ans) } \ No newline at end of file diff --git a/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/Solution.go b/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/Solution.go index 1ad9840903faa..d60cadda01462 100644 --- a/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/Solution.go +++ b/solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/Solution.go @@ -1,32 +1,32 @@ -func minimumRelativeLosses(prices []int, queries [][]int) []int64 { - n := len(prices) - sort.Ints(prices) - s := make([]int, n+1) - for i, x := range prices { - s[i+1] = s[i] + x - } - f := func(k, m int) int { - l, r := 0, sort.Search(n, func(i int) bool { return prices[i] > k }) - if r > m { - r = m - } - for l < r { - mid := (l + r) >> 1 - right := m - mid - if prices[mid] < 2*k-prices[n-right] { - l = mid + 1 - } else { - r = mid - } - } - return l - } - ans := make([]int64, len(queries)) - for i, q := range queries { - k, m := q[0], q[1] - l := f(k, m) - r := m - l - ans[i] = int64(s[l] + 2*k*r - (s[n] - s[n-r])) - } - return ans +func minimumRelativeLosses(prices []int, queries [][]int) []int64 { + n := len(prices) + sort.Ints(prices) + s := make([]int, n+1) + for i, x := range prices { + s[i+1] = s[i] + x + } + f := func(k, m int) int { + l, r := 0, sort.Search(n, func(i int) bool { return prices[i] > k }) + if r > m { + r = m + } + for l < r { + mid := (l + r) >> 1 + right := m - mid + if prices[mid] < 2*k-prices[n-right] { + l = mid + 1 + } else { + r = mid + } + } + return l + } + ans := make([]int64, len(queries)) + for i, q := range queries { + k, m := q[0], q[1] + l := f(k, m) + r := m - l + ans[i] = int64(s[l] + 2*k*r - (s[n] - s[n-r])) + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2828.Check if a String Is an Acronym of Words/Solution.go b/solution/2800-2899/2828.Check if a String Is an Acronym of Words/Solution.go index 5c562df36c0b7..d988e77097bec 100644 --- a/solution/2800-2899/2828.Check if a String Is an Acronym of Words/Solution.go +++ b/solution/2800-2899/2828.Check if a String Is an Acronym of Words/Solution.go @@ -1,7 +1,7 @@ -func isAcronym(words []string, s string) bool { - t := []byte{} - for _, w := range words { - t = append(t, w[0]) - } - return string(t) == s +func isAcronym(words []string, s string) bool { + t := []byte{} + for _, w := range words { + t = append(t, w[0]) + } + return string(t) == s } \ No newline at end of file diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go index 3ceacec441460..0815dc8d621b8 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go @@ -1,15 +1,15 @@ -func minimumSum(n int, k int) int { - s, i := 0, 1 - vis := make([]bool, k+n*n+1) - for ; n > 0; n-- { - for vis[i] { - i++ - } - vis[i] = true - if k >= i { - vis[k-i] = true - } - s += i - } - return s +func minimumSum(n int, k int) int { + s, i := 0, 1 + vis := make([]bool, k+n*n+1) + for ; n > 0; n-- { + for vis[i] { + i++ + } + vis[i] = true + if k >= i { + vis[k-i] = true + } + s += i + } + return s } \ No newline at end of file diff --git a/solution/2800-2899/2830.Maximize the Profit as the Salesman/Solution.go b/solution/2800-2899/2830.Maximize the Profit as the Salesman/Solution.go index 3127b18b25fe7..87f033410dca5 100644 --- a/solution/2800-2899/2830.Maximize the Profit as the Salesman/Solution.go +++ b/solution/2800-2899/2830.Maximize the Profit as the Salesman/Solution.go @@ -1,14 +1,14 @@ -func maximizeTheProfit(n int, offers [][]int) int { - sort.Slice(offers, func(i, j int) bool { return offers[i][1] < offers[j][1] }) - n = len(offers) - f := make([]int, n+1) - g := []int{} - for _, o := range offers { - g = append(g, o[1]) - } - for i := 1; i <= n; i++ { - j := sort.SearchInts(g, offers[i-1][0]) - f[i] = max(f[i-1], f[j]+offers[i-1][2]) - } - return f[n] +func maximizeTheProfit(n int, offers [][]int) int { + sort.Slice(offers, func(i, j int) bool { return offers[i][1] < offers[j][1] }) + n = len(offers) + f := make([]int, n+1) + g := []int{} + for _, o := range offers { + g = append(g, o[1]) + } + for i := 1; i <= n; i++ { + j := sort.SearchInts(g, offers[i-1][0]) + f[i] = max(f[i-1], f[j]+offers[i-1][2]) + } + return f[n] } \ No newline at end of file diff --git a/solution/2800-2899/2833.Furthest Point From Origin/Solution.go b/solution/2800-2899/2833.Furthest Point From Origin/Solution.go index 430e1fe89d61b..a9967164cc73e 100644 --- a/solution/2800-2899/2833.Furthest Point From Origin/Solution.go +++ b/solution/2800-2899/2833.Furthest Point From Origin/Solution.go @@ -1,11 +1,11 @@ -func furthestDistanceFromOrigin(moves string) int { - count := func(c string) int { return strings.Count(moves, c) } - return abs(count("L")-count("R")) + count("_") -} - -func abs(x int) int { - if x < 0 { - return -x - } - return x +func furthestDistanceFromOrigin(moves string) int { + count := func(c string) int { return strings.Count(moves, c) } + return abs(count("L")-count("R")) + count("_") +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x } \ No newline at end of file diff --git a/solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/Solution.go b/solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/Solution.go index e20fc28b0925c..bb90f50fd79f0 100644 --- a/solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/Solution.go +++ b/solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/Solution.go @@ -1,13 +1,13 @@ -func minimumPossibleSum(n int, target int) (ans int64) { - vis := make([]bool, n+target) - for i := 1; n > 0; i, n = i+1, n-1 { - for vis[i] { - i++ - } - ans += int64(i) - if target >= i { - vis[target-i] = true - } - } - return +func minimumPossibleSum(n int, target int) (ans int64) { + vis := make([]bool, n+target) + for i := 1; n > 0; i, n = i+1, n-1 { + for vis[i] { + i++ + } + ans += int64(i) + if target >= i { + vis[target-i] = true + } + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2835.Minimum Operations to Form Subsequence With Target Sum/Solution.go b/solution/2800-2899/2835.Minimum Operations to Form Subsequence With Target Sum/Solution.go index 923cbf6b901d0..703e4066f7e2f 100644 --- a/solution/2800-2899/2835.Minimum Operations to Form Subsequence With Target Sum/Solution.go +++ b/solution/2800-2899/2835.Minimum Operations to Form Subsequence With Target Sum/Solution.go @@ -1,37 +1,37 @@ -func minOperations(nums []int, target int) (ans int) { - s := 0 - cnt := [32]int{} - for _, x := range nums { - s += x - for i := 0; i < 32; i++ { - if x>>i&1 > 0 { - cnt[i]++ - } - } - } - if s < target { - return -1 - } - var i, j int - for { - for i < 32 && target>>i&1 == 0 { - i++ - } - if i == 32 { - return - } - for j < i { - cnt[j+1] += cnt[j] >> 1 - cnt[j] %= 2 - j++ - } - for cnt[j] == 0 { - cnt[j] = 1 - j++ - } - ans += j - i - cnt[j]-- - j = i - i++ - } +func minOperations(nums []int, target int) (ans int) { + s := 0 + cnt := [32]int{} + for _, x := range nums { + s += x + for i := 0; i < 32; i++ { + if x>>i&1 > 0 { + cnt[i]++ + } + } + } + if s < target { + return -1 + } + var i, j int + for { + for i < 32 && target>>i&1 == 0 { + i++ + } + if i == 32 { + return + } + for j < i { + cnt[j+1] += cnt[j] >> 1 + cnt[j] %= 2 + j++ + } + for cnt[j] == 0 { + cnt[j] = 1 + j++ + } + ans += j - i + cnt[j]-- + j = i + i++ + } } \ No newline at end of file diff --git a/solution/2800-2899/2836.Maximize Value of Function in a Ball Passing Game/Solution.go b/solution/2800-2899/2836.Maximize Value of Function in a Ball Passing Game/Solution.go index 90de3cb6afacf..325f35a74e851 100644 --- a/solution/2800-2899/2836.Maximize Value of Function in a Ball Passing Game/Solution.go +++ b/solution/2800-2899/2836.Maximize Value of Function in a Ball Passing Game/Solution.go @@ -1,29 +1,29 @@ -func getMaxFunctionValue(receiver []int, k int64) (ans int64) { - n, m := len(receiver), bits.Len(uint(k)) - f := make([][]int, n) - g := make([][]int64, n) - for i := range f { - f[i] = make([]int, m) - g[i] = make([]int64, m) - f[i][0] = receiver[i] - g[i][0] = int64(i) - } - for j := 1; j < m; j++ { - for i := 0; i < n; i++ { - f[i][j] = f[f[i][j-1]][j-1] - g[i][j] = g[i][j-1] + g[f[i][j-1]][j-1] - } - } - for i := 0; i < n; i++ { - p := i - t := int64(0) - for j := 0; j < m; j++ { - if k>>j&1 == 1 { - t += g[p][j] - p = f[p][j] - } - } - ans = max(ans, t+int64(p)) - } - return +func getMaxFunctionValue(receiver []int, k int64) (ans int64) { + n, m := len(receiver), bits.Len(uint(k)) + f := make([][]int, n) + g := make([][]int64, n) + for i := range f { + f[i] = make([]int, m) + g[i] = make([]int64, m) + f[i][0] = receiver[i] + g[i][0] = int64(i) + } + for j := 1; j < m; j++ { + for i := 0; i < n; i++ { + f[i][j] = f[f[i][j-1]][j-1] + g[i][j] = g[i][j-1] + g[f[i][j-1]][j-1] + } + } + for i := 0; i < n; i++ { + p := i + t := int64(0) + for j := 0; j < m; j++ { + if k>>j&1 == 1 { + t += g[p][j] + p = f[p][j] + } + } + ans = max(ans, t+int64(p)) + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/Solution.go b/solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/Solution.go index 7c2853dd11de4..120e61f17cf7d 100644 --- a/solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/Solution.go +++ b/solution/2800-2899/2839.Check if Strings Can be Made Equal With Operations I/Solution.go @@ -1,13 +1,13 @@ -func canBeEqual(s1 string, s2 string) bool { - cnt := [2][26]int{} - for i := 0; i < len(s1); i++ { - cnt[i&1][s1[i]-'a']++ - cnt[i&1][s2[i]-'a']-- - } - for i := 0; i < 26; i++ { - if cnt[0][i] != 0 || cnt[1][i] != 0 { - return false - } - } - return true +func canBeEqual(s1 string, s2 string) bool { + cnt := [2][26]int{} + for i := 0; i < len(s1); i++ { + cnt[i&1][s1[i]-'a']++ + cnt[i&1][s2[i]-'a']-- + } + for i := 0; i < 26; i++ { + if cnt[0][i] != 0 || cnt[1][i] != 0 { + return false + } + } + return true } \ No newline at end of file diff --git a/solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/Solution.go b/solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/Solution.go index 895c27e500b24..ab360adbc7048 100644 --- a/solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/Solution.go +++ b/solution/2800-2899/2840.Check if Strings Can be Made Equal With Operations II/Solution.go @@ -1,13 +1,13 @@ -func checkStrings(s1 string, s2 string) bool { - cnt := [2][26]int{} - for i := 0; i < len(s1); i++ { - cnt[i&1][s1[i]-'a']++ - cnt[i&1][s2[i]-'a']-- - } - for i := 0; i < 26; i++ { - if cnt[0][i] != 0 || cnt[1][i] != 0 { - return false - } - } - return true +func checkStrings(s1 string, s2 string) bool { + cnt := [2][26]int{} + for i := 0; i < len(s1); i++ { + cnt[i&1][s1[i]-'a']++ + cnt[i&1][s2[i]-'a']-- + } + for i := 0; i < 26; i++ { + if cnt[0][i] != 0 || cnt[1][i] != 0 { + return false + } + } + return true } \ No newline at end of file diff --git a/solution/2800-2899/2841.Maximum Sum of Almost Unique Subarray/Solution.go b/solution/2800-2899/2841.Maximum Sum of Almost Unique Subarray/Solution.go index 42fc4ceb189c7..70775744a7b5a 100644 --- a/solution/2800-2899/2841.Maximum Sum of Almost Unique Subarray/Solution.go +++ b/solution/2800-2899/2841.Maximum Sum of Almost Unique Subarray/Solution.go @@ -1,24 +1,24 @@ -func maxSum(nums []int, m int, k int) int64 { - cnt := map[int]int{} - var s int64 - for _, x := range nums[:k] { - cnt[x]++ - s += int64(x) - } - var ans int64 - if len(cnt) >= m { - ans = s - } - for i := k; i < len(nums); i++ { - cnt[nums[i]]++ - cnt[nums[i-k]]-- - if cnt[nums[i-k]] == 0 { - delete(cnt, nums[i-k]) - } - s += int64(nums[i]) - int64(nums[i-k]) - if len(cnt) >= m { - ans = max(ans, s) - } - } - return ans +func maxSum(nums []int, m int, k int) int64 { + cnt := map[int]int{} + var s int64 + for _, x := range nums[:k] { + cnt[x]++ + s += int64(x) + } + var ans int64 + if len(cnt) >= m { + ans = s + } + for i := k; i < len(nums); i++ { + cnt[nums[i]]++ + cnt[nums[i-k]]-- + if cnt[nums[i-k]] == 0 { + delete(cnt, nums[i-k]) + } + s += int64(nums[i]) - int64(nums[i-k]) + if len(cnt) >= m { + ans = max(ans, s) + } + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2842.Count K-Subsequences of a String With Maximum Beauty/Solution.go b/solution/2800-2899/2842.Count K-Subsequences of a String With Maximum Beauty/Solution.go index 1a3ec16a68899..fd3bde3934502 100644 --- a/solution/2800-2899/2842.Count K-Subsequences of a String With Maximum Beauty/Solution.go +++ b/solution/2800-2899/2842.Count K-Subsequences of a String With Maximum Beauty/Solution.go @@ -1,58 +1,58 @@ -func countKSubsequencesWithMaxBeauty(s string, k int) int { - f := [26]int{} - cnt := 0 - for _, c := range s { - f[c-'a']++ - if f[c-'a'] == 1 { - cnt++ - } - } - if cnt < k { - return 0 - } - vs := []int{} - for _, x := range f { - if x > 0 { - vs = append(vs, x) - } - } - sort.Slice(vs, func(i, j int) bool { - return vs[i] > vs[j] - }) - const mod int = 1e9 + 7 - ans := 1 - val := vs[k-1] - x := 0 - for _, v := range vs { - if v == val { - x++ - } - } - for _, v := range vs { - if v == val { - break - } - k-- - ans = ans * v % mod - } - c := make([][]int, x+1) - for i := range c { - c[i] = make([]int, x+1) - c[i][0] = 1 - for j := 1; j <= i; j++ { - c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod - } - } - qpow := func(a, n int) int { - ans := 1 - for ; n > 0; n >>= 1 { - if n&1 == 1 { - ans = ans * a % mod - } - a = a * a % mod - } - return ans - } - ans = (ans * c[x][k] % mod) * qpow(val, k) % mod - return ans +func countKSubsequencesWithMaxBeauty(s string, k int) int { + f := [26]int{} + cnt := 0 + for _, c := range s { + f[c-'a']++ + if f[c-'a'] == 1 { + cnt++ + } + } + if cnt < k { + return 0 + } + vs := []int{} + for _, x := range f { + if x > 0 { + vs = append(vs, x) + } + } + sort.Slice(vs, func(i, j int) bool { + return vs[i] > vs[j] + }) + const mod int = 1e9 + 7 + ans := 1 + val := vs[k-1] + x := 0 + for _, v := range vs { + if v == val { + x++ + } + } + for _, v := range vs { + if v == val { + break + } + k-- + ans = ans * v % mod + } + c := make([][]int, x+1) + for i := range c { + c[i] = make([]int, x+1) + c[i][0] = 1 + for j := 1; j <= i; j++ { + c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod + } + } + qpow := func(a, n int) int { + ans := 1 + for ; n > 0; n >>= 1 { + if n&1 == 1 { + ans = ans * a % mod + } + a = a * a % mod + } + return ans + } + ans = (ans * c[x][k] % mod) * qpow(val, k) % mod + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2846.Minimum Edge Weight Equilibrium Queries in a Tree/Solution.go b/solution/2800-2899/2846.Minimum Edge Weight Equilibrium Queries in a Tree/Solution.go index 80ace9a4e8ee4..b3551f1681b4d 100644 --- a/solution/2800-2899/2846.Minimum Edge Weight Equilibrium Queries in a Tree/Solution.go +++ b/solution/2800-2899/2846.Minimum Edge Weight Equilibrium Queries in a Tree/Solution.go @@ -1,66 +1,66 @@ -func minOperationsQueries(n int, edges [][]int, queries [][]int) []int { - m := bits.Len(uint(n)) - g := make([][][2]int, n) - f := make([][]int, n) - for i := range f { - f[i] = make([]int, m) - } - p := make([]int, n) - cnt := make([][26]int, n) - cnt[0] = [26]int{} - depth := make([]int, n) - for _, e := range edges { - u, v, w := e[0], e[1], e[2]-1 - g[u] = append(g[u], [2]int{v, w}) - g[v] = append(g[v], [2]int{u, w}) - } - q := []int{0} - for len(q) > 0 { - i := q[0] - q = q[1:] - f[i][0] = p[i] - for j := 1; j < m; j++ { - f[i][j] = f[f[i][j-1]][j-1] - } - for _, nxt := range g[i] { - j, w := nxt[0], nxt[1] - if j != p[i] { - p[j] = i - cnt[j] = [26]int{} - for k := 0; k < 26; k++ { - cnt[j][k] = cnt[i][k] - } - cnt[j][w]++ - depth[j] = depth[i] + 1 - q = append(q, j) - } - } - } - ans := make([]int, len(queries)) - for i, qq := range queries { - u, v := qq[0], qq[1] - x, y := u, v - if depth[x] < depth[y] { - x, y = y, x - } - for j := m - 1; j >= 0; j-- { - if depth[x]-depth[y] >= (1 << j) { - x = f[x][j] - } - } - for j := m - 1; j >= 0; j-- { - if f[x][j] != f[y][j] { - x, y = f[x][j], f[y][j] - } - } - if x != y { - x = p[x] - } - mx := 0 - for j := 0; j < 26; j++ { - mx = max(mx, cnt[u][j]+cnt[v][j]-2*cnt[x][j]) - } - ans[i] = depth[u] + depth[v] - 2*depth[x] - mx - } - return ans +func minOperationsQueries(n int, edges [][]int, queries [][]int) []int { + m := bits.Len(uint(n)) + g := make([][][2]int, n) + f := make([][]int, n) + for i := range f { + f[i] = make([]int, m) + } + p := make([]int, n) + cnt := make([][26]int, n) + cnt[0] = [26]int{} + depth := make([]int, n) + for _, e := range edges { + u, v, w := e[0], e[1], e[2]-1 + g[u] = append(g[u], [2]int{v, w}) + g[v] = append(g[v], [2]int{u, w}) + } + q := []int{0} + for len(q) > 0 { + i := q[0] + q = q[1:] + f[i][0] = p[i] + for j := 1; j < m; j++ { + f[i][j] = f[f[i][j-1]][j-1] + } + for _, nxt := range g[i] { + j, w := nxt[0], nxt[1] + if j != p[i] { + p[j] = i + cnt[j] = [26]int{} + for k := 0; k < 26; k++ { + cnt[j][k] = cnt[i][k] + } + cnt[j][w]++ + depth[j] = depth[i] + 1 + q = append(q, j) + } + } + } + ans := make([]int, len(queries)) + for i, qq := range queries { + u, v := qq[0], qq[1] + x, y := u, v + if depth[x] < depth[y] { + x, y = y, x + } + for j := m - 1; j >= 0; j-- { + if depth[x]-depth[y] >= (1 << j) { + x = f[x][j] + } + } + for j := m - 1; j >= 0; j-- { + if f[x][j] != f[y][j] { + x, y = f[x][j], f[y][j] + } + } + if x != y { + x = p[x] + } + mx := 0 + for j := 0; j < 26; j++ { + mx = max(mx, cnt[u][j]+cnt[v][j]-2*cnt[x][j]) + } + ans[i] = depth[u] + depth[v] - 2*depth[x] - mx + } + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2847.Smallest Number With Given Digit Product/Solution.go b/solution/2800-2899/2847.Smallest Number With Given Digit Product/Solution.go index 63e4c9281d201..9bd280fa7cb53 100644 --- a/solution/2800-2899/2847.Smallest Number With Given Digit Product/Solution.go +++ b/solution/2800-2899/2847.Smallest Number With Given Digit Product/Solution.go @@ -1,23 +1,23 @@ -func smallestNumber(n int64) string { - cnt := [10]int{} - for i := 9; i > 1; i-- { - for n%int64(i) == 0 { - cnt[i]++ - n /= int64(i) - } - } - if n != 1 { - return "-1" - } - sb := &strings.Builder{} - for i := 2; i < 10; i++ { - for j := 0; j < cnt[i]; j++ { - sb.WriteByte(byte(i) + '0') - } - } - ans := sb.String() - if len(ans) > 0 { - return ans - } - return "1" +func smallestNumber(n int64) string { + cnt := [10]int{} + for i := 9; i > 1; i-- { + for n%int64(i) == 0 { + cnt[i]++ + n /= int64(i) + } + } + if n != 1 { + return "-1" + } + sb := &strings.Builder{} + for i := 2; i < 10; i++ { + for j := 0; j < cnt[i]; j++ { + sb.WriteByte(byte(i) + '0') + } + } + ans := sb.String() + if len(ans) > 0 { + return ans + } + return "1" } \ No newline at end of file diff --git a/solution/2800-2899/2848.Points That Intersect With Cars/Solution.go b/solution/2800-2899/2848.Points That Intersect With Cars/Solution.go index 1114ddab2a8b2..7c606d431d4ba 100644 --- a/solution/2800-2899/2848.Points That Intersect With Cars/Solution.go +++ b/solution/2800-2899/2848.Points That Intersect With Cars/Solution.go @@ -1,15 +1,15 @@ -func numberOfPoints(nums [][]int) (ans int) { - d := [110]int{} - for _, e := range nums { - d[e[0]]++ - d[e[1]+1]-- - } - s := 0 - for _, x := range d { - s += x - if s > 0 { - ans++ - } - } - return +func numberOfPoints(nums [][]int) (ans int) { + d := [110]int{} + for _, e := range nums { + d[e[0]]++ + d[e[1]+1]-- + } + s := 0 + for _, x := range d { + s += x + if s > 0 { + ans++ + } + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/Solution.go b/solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/Solution.go index 520a07b621bef..37f10deacfd4a 100644 --- a/solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/Solution.go +++ b/solution/2800-2899/2849.Determine if a Cell Is Reachable at a Given Time/Solution.go @@ -1,15 +1,15 @@ -func isReachableAtTime(sx int, sy int, fx int, fy int, t int) bool { - if sx == fx && sy == fy { - return t != 1 - } - dx := abs(sx - fx) - dy := abs(sy - fy) - return max(dx, dy) <= t -} - -func abs(x int) int { - if x < 0 { - return -x - } - return x +func isReachableAtTime(sx int, sy int, fx int, fy int, t int) bool { + if sx == fx && sy == fy { + return t != 1 + } + dx := abs(sx - fx) + dy := abs(sy - fy) + return max(dx, dy) <= t +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x } \ No newline at end of file diff --git a/solution/2800-2899/2850.Minimum Moves to Spread Stones Over Grid/Solution.go b/solution/2800-2899/2850.Minimum Moves to Spread Stones Over Grid/Solution.go index 413659456b246..80d36b4dac719 100644 --- a/solution/2800-2899/2850.Minimum Moves to Spread Stones Over Grid/Solution.go +++ b/solution/2800-2899/2850.Minimum Moves to Spread Stones Over Grid/Solution.go @@ -1,38 +1,38 @@ -func minimumMoves(grid [][]int) int { - left := [][2]int{} - right := [][2]int{} - for i := 0; i < 3; i++ { - for j := 0; j < 3; j++ { - if grid[i][j] == 0 { - left = append(left, [2]int{i, j}) - } else { - for k := 1; k < grid[i][j]; k++ { - right = append(right, [2]int{i, j}) - } - } - } - } - cal := func(a, b [2]int) int { - return abs(a[0]-b[0]) + abs(a[1]-b[1]) - } - n := len(left) - f := make([]int, 1<>j&1 == 1 { - f[i] = min(f[i], f[i^(1<>j&1 == 1 { + f[i] = min(f[i], f[i^(1< 1 { - x, y := h.pop(), h.pop() - if x > 1 { - h.push(x - 1) - } - if y > 1 { - h.push(y - 1) - } - ans -= 2 - } - return ans -} - -type hp struct{ sort.IntSlice } - -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -func (h *hp) push(v int) { heap.Push(h, v) } +func minLengthAfterRemovals(nums []int) int { + cnt := map[int]int{} + for _, x := range nums { + cnt[x]++ + } + h := &hp{} + for _, x := range cnt { + h.push(x) + } + ans := len(nums) + for h.Len() > 1 { + x, y := h.pop(), h.pop() + if x > 1 { + h.push(x - 1) + } + if y > 1 { + h.push(y - 1) + } + ans -= 2 + } + return ans +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +func (h *hp) push(v int) { heap.Push(h, v) } func (h *hp) pop() int { return heap.Pop(h).(int) } \ No newline at end of file diff --git a/solution/2800-2899/2857.Count Pairs of Points With Distance k/Soution.go b/solution/2800-2899/2857.Count Pairs of Points With Distance k/Soution.go index 0cb2171551372..27151bcbd5e3b 100644 --- a/solution/2800-2899/2857.Count Pairs of Points With Distance k/Soution.go +++ b/solution/2800-2899/2857.Count Pairs of Points With Distance k/Soution.go @@ -1,13 +1,13 @@ -func countPairs(coordinates [][]int, k int) (ans int) { - cnt := map[[2]int]int{} - for _, c := range coordinates { - x2, y2 := c[0], c[1] - for a := 0; a <= k; a++ { - b := k - a - x1, y1 := a^x2, b^y2 - ans += cnt[[2]int{x1, y1}] - } - cnt[[2]int{x2, y2}]++ - } - return +func countPairs(coordinates [][]int, k int) (ans int) { + cnt := map[[2]int]int{} + for _, c := range coordinates { + x2, y2 := c[0], c[1] + for a := 0; a <= k; a++ { + b := k - a + x1, y1 := a^x2, b^y2 + ans += cnt[[2]int{x1, y1}] + } + cnt[[2]int{x2, y2}]++ + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2858.Minimum Edge Reversals So Every Node Is Reachable/Solution.go b/solution/2800-2899/2858.Minimum Edge Reversals So Every Node Is Reachable/Solution.go index b9fa738f376f1..2266515fcf92c 100644 --- a/solution/2800-2899/2858.Minimum Edge Reversals So Every Node Is Reachable/Solution.go +++ b/solution/2800-2899/2858.Minimum Edge Reversals So Every Node Is Reachable/Solution.go @@ -1,34 +1,34 @@ -func minEdgeReversals(n int, edges [][]int) []int { - g := make([][][2]int, n) - for _, e := range edges { - x, y := e[0], e[1] - g[x] = append(g[x], [2]int{y, 1}) - g[y] = append(g[y], [2]int{x, -1}) - } - ans := make([]int, n) - var dfs func(int, int) - var dfs2 func(int, int) - dfs = func(i, fa int) { - for _, ne := range g[i] { - j, k := ne[0], ne[1] - if j != fa { - if k < 0 { - ans[0]++ - } - dfs(j, i) - } - } - } - dfs2 = func(i, fa int) { - for _, ne := range g[i] { - j, k := ne[0], ne[1] - if j != fa { - ans[j] = ans[i] + k - dfs2(j, i) - } - } - } - dfs(0, -1) - dfs2(0, -1) - return ans +func minEdgeReversals(n int, edges [][]int) []int { + g := make([][][2]int, n) + for _, e := range edges { + x, y := e[0], e[1] + g[x] = append(g[x], [2]int{y, 1}) + g[y] = append(g[y], [2]int{x, -1}) + } + ans := make([]int, n) + var dfs func(int, int) + var dfs2 func(int, int) + dfs = func(i, fa int) { + for _, ne := range g[i] { + j, k := ne[0], ne[1] + if j != fa { + if k < 0 { + ans[0]++ + } + dfs(j, i) + } + } + } + dfs2 = func(i, fa int) { + for _, ne := range g[i] { + j, k := ne[0], ne[1] + if j != fa { + ans[j] = ans[i] + k + dfs2(j, i) + } + } + } + dfs(0, -1) + dfs2(0, -1) + return ans } \ No newline at end of file diff --git a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go index 149bbc17d2e80..c141c0e8007e4 100644 --- a/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go +++ b/solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/Solution.go @@ -1,8 +1,8 @@ -func sumIndicesWithKSetBits(nums []int, k int) (ans int) { - for i, x := range nums { - if bits.OnesCount(uint(i)) == k { - ans += x - } - } - return +func sumIndicesWithKSetBits(nums []int, k int) (ans int) { + for i, x := range nums { + if bits.OnesCount(uint(i)) == k { + ans += x + } + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2860.Happy Students/Solution.go b/solution/2800-2899/2860.Happy Students/Solution.go index 40bdd8d427c50..ea6e83edcedbb 100644 --- a/solution/2800-2899/2860.Happy Students/Solution.go +++ b/solution/2800-2899/2860.Happy Students/Solution.go @@ -1,11 +1,11 @@ -func countWays(nums []int) (ans int) { - sort.Ints(nums) - n := len(nums) - for i := 0; i <= n; i++ { - if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { - continue - } - ans++ - } - return +func countWays(nums []int) (ans int) { + sort.Ints(nums) + n := len(nums) + for i := 0; i <= n; i++ { + if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) { + continue + } + ans++ + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go index 425df3919a225..4f8420d9e1235 100644 --- a/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go +++ b/solution/2800-2899/2861.Maximum Number of Alloys/Solution.go @@ -1,26 +1,26 @@ -func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int { - isValid := func(target int) bool { - for _, currMachine := range composition { - remain := budget - for i, x := range currMachine { - need := max(0, x*target-stock[i]) - remain -= need * cost[i] - } - if remain >= 0 { - return true - } - } - return false - } - - l, r := 0, budget+stock[0] - for l < r { - mid := (l + r + 1) >> 1 - if isValid(mid) { - l = mid - } else { - r = mid - 1 - } - } - return l +func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int { + isValid := func(target int) bool { + for _, currMachine := range composition { + remain := budget + for i, x := range currMachine { + need := max(0, x*target-stock[i]) + remain -= need * cost[i] + } + if remain >= 0 { + return true + } + } + return false + } + + l, r := 0, budget+stock[0] + for l < r { + mid := (l + r + 1) >> 1 + if isValid(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l } \ No newline at end of file diff --git a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.go b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.go index ffb1e071712b1..cd156cb72e49b 100644 --- a/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.go +++ b/solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/Solution.go @@ -1,11 +1,11 @@ -func maximumSum(nums []int) (ans int64) { - n := len(nums) - for k := 1; k <= n; k++ { - var t int64 - for j := 1; k*j*j <= n; j++ { - t += int64(nums[k*j*j-1]) - } - ans = max(ans, t) - } - return +func maximumSum(nums []int) (ans int64) { + n := len(nums) + for k := 1; k <= n; k++ { + var t int64 + for j := 1; k*j*j <= n; j++ { + t += int64(nums[k*j*j-1]) + } + ans = max(ans, t) + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go index 2782713930c24..86f1bb7dd8887 100644 --- a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go @@ -1,4 +1,4 @@ -func maximumOddBinaryNumber(s string) string { - cnt := strings.Count(s, "1") - return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" +func maximumOddBinaryNumber(s string) string { + cnt := strings.Count(s, "1") + return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" } \ No newline at end of file diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.go b/solution/2800-2899/2865.Beautiful Towers I/Solution.go index f1edad00a9dd0..696b64f7d56db 100644 --- a/solution/2800-2899/2865.Beautiful Towers I/Solution.go +++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.go @@ -1,17 +1,17 @@ -func maximumSumOfHeights(maxHeights []int) (ans int64) { - n := len(maxHeights) - for i, x := range maxHeights { - y, t := x, x - for j := i - 1; j >= 0; j-- { - y = min(y, maxHeights[j]) - t += y - } - y = x - for j := i + 1; j < n; j++ { - y = min(y, maxHeights[j]) - t += y - } - ans = max(ans, int64(t)) - } - return +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + for i, x := range maxHeights { + y, t := x, x + for j := i - 1; j >= 0; j-- { + y = min(y, maxHeights[j]) + t += y + } + y = x + for j := i + 1; j < n; j++ { + y = min(y, maxHeights[j]) + t += y + } + ans = max(ans, int64(t)) + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.go b/solution/2800-2899/2866.Beautiful Towers II/Solution.go index c5e387fe00ad1..25e58174dd015 100644 --- a/solution/2800-2899/2866.Beautiful Towers II/Solution.go +++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.go @@ -1,59 +1,59 @@ -func maximumSumOfHeights(maxHeights []int) (ans int64) { - n := len(maxHeights) - stk := []int{} - left := make([]int, n) - right := make([]int, n) - for i := range left { - left[i] = -1 - right[i] = n - } - for i, x := range maxHeights { - for len(stk) > 0 && maxHeights[stk[len(stk)-1]] > x { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - left[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - stk = []int{} - for i := n - 1; i >= 0; i-- { - x := maxHeights[i] - for len(stk) > 0 && maxHeights[stk[len(stk)-1]] >= x { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - right[i] = stk[len(stk)-1] - } - stk = append(stk, i) - } - f := make([]int64, n) - g := make([]int64, n) - for i, x := range maxHeights { - if i > 0 && x >= maxHeights[i-1] { - f[i] = f[i-1] + int64(x) - } else { - j := left[i] - f[i] = int64(x) * int64(i-j) - if j != -1 { - f[i] += f[j] - } - } - } - for i := n - 1; i >= 0; i-- { - x := maxHeights[i] - if i < n-1 && x >= maxHeights[i+1] { - g[i] = g[i+1] + int64(x) - } else { - j := right[i] - g[i] = int64(x) * int64(j-i) - if j != n { - g[i] += g[j] - } - } - } - for i, x := range maxHeights { - ans = max(ans, f[i]+g[i]-int64(x)) - } - return +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + stk := []int{} + left := make([]int, n) + right := make([]int, n) + for i := range left { + left[i] = -1 + right[i] = n + } + for i, x := range maxHeights { + for len(stk) > 0 && maxHeights[stk[len(stk)-1]] > x { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + left[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + stk = []int{} + for i := n - 1; i >= 0; i-- { + x := maxHeights[i] + for len(stk) > 0 && maxHeights[stk[len(stk)-1]] >= x { + stk = stk[:len(stk)-1] + } + if len(stk) > 0 { + right[i] = stk[len(stk)-1] + } + stk = append(stk, i) + } + f := make([]int64, n) + g := make([]int64, n) + for i, x := range maxHeights { + if i > 0 && x >= maxHeights[i-1] { + f[i] = f[i-1] + int64(x) + } else { + j := left[i] + f[i] = int64(x) * int64(i-j) + if j != -1 { + f[i] += f[j] + } + } + } + for i := n - 1; i >= 0; i-- { + x := maxHeights[i] + if i < n-1 && x >= maxHeights[i+1] { + g[i] = g[i+1] + int64(x) + } else { + j := right[i] + g[i] = int64(x) * int64(j-i) + if j != n { + g[i] += g[j] + } + } + } + for i, x := range maxHeights { + ans = max(ans, f[i]+g[i]-int64(x)) + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2867.Count Valid Paths in a Tree/Solution.go b/solution/2800-2899/2867.Count Valid Paths in a Tree/Solution.go index e7f6f397bca92..17ab4802a6c28 100644 --- a/solution/2800-2899/2867.Count Valid Paths in a Tree/Solution.go +++ b/solution/2800-2899/2867.Count Valid Paths in a Tree/Solution.go @@ -1,82 +1,82 @@ -const mx int = 1e5 + 10 - -var prime [mx]bool - -func init() { - for i := 2; i < mx; i++ { - prime[i] = true - } - for i := 2; i < mx; i++ { - if prime[i] { - for j := i + i; j < mx; j += i { - prime[j] = false - } - } - } -} - -type unionFind struct { - p, size []int -} - -func newUnionFind(n int) *unionFind { - p := make([]int, n) - size := make([]int, n) - for i := range p { - p[i] = i - size[i] = 1 - } - return &unionFind{p, size} -} - -func (uf *unionFind) find(x int) int { - if uf.p[x] != x { - uf.p[x] = uf.find(uf.p[x]) - } - return uf.p[x] -} - -func (uf *unionFind) union(a, b int) bool { - pa, pb := uf.find(a), uf.find(b) - if pa == pb { - return false - } - if uf.size[pa] > uf.size[pb] { - uf.p[pb] = pa - uf.size[pa] += uf.size[pb] - } else { - uf.p[pa] = pb - uf.size[pb] += uf.size[pa] - } - return true -} - -func (uf *unionFind) getSize(x int) int { - return uf.size[uf.find(x)] -} - -func countPaths(n int, edges [][]int) (ans int64) { - uf := newUnionFind(n + 1) - g := make([][]int, n+1) - for _, e := range edges { - u, v := e[0], e[1] - g[u] = append(g[u], v) - g[v] = append(g[v], u) - if !prime[u] && !prime[v] { - uf.union(u, v) - } - } - for i := 1; i <= n; i++ { - if prime[i] { - t := 0 - for _, j := range g[i] { - if !prime[j] { - cnt := uf.getSize(j) - ans += int64(cnt + cnt*t) - t += cnt - } - } - } - } - return +const mx int = 1e5 + 10 + +var prime [mx]bool + +func init() { + for i := 2; i < mx; i++ { + prime[i] = true + } + for i := 2; i < mx; i++ { + if prime[i] { + for j := i + i; j < mx; j += i { + prime[j] = false + } + } + } +} + +type unionFind struct { + p, size []int +} + +func newUnionFind(n int) *unionFind { + p := make([]int, n) + size := make([]int, n) + for i := range p { + p[i] = i + size[i] = 1 + } + return &unionFind{p, size} +} + +func (uf *unionFind) find(x int) int { + if uf.p[x] != x { + uf.p[x] = uf.find(uf.p[x]) + } + return uf.p[x] +} + +func (uf *unionFind) union(a, b int) bool { + pa, pb := uf.find(a), uf.find(b) + if pa == pb { + return false + } + if uf.size[pa] > uf.size[pb] { + uf.p[pb] = pa + uf.size[pa] += uf.size[pb] + } else { + uf.p[pa] = pb + uf.size[pb] += uf.size[pa] + } + return true +} + +func (uf *unionFind) getSize(x int) int { + return uf.size[uf.find(x)] +} + +func countPaths(n int, edges [][]int) (ans int64) { + uf := newUnionFind(n + 1) + g := make([][]int, n+1) + for _, e := range edges { + u, v := e[0], e[1] + g[u] = append(g[u], v) + g[v] = append(g[v], u) + if !prime[u] && !prime[v] { + uf.union(u, v) + } + } + for i := 1; i <= n; i++ { + if prime[i] { + t := 0 + for _, j := range g[i] { + if !prime[j] { + cnt := uf.getSize(j) + ans += int64(cnt + cnt*t) + t += cnt + } + } + } + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/Solution.go b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/Solution.go index 9a960809266a0..87094c9638aeb 100644 --- a/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/Solution.go +++ b/solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/Solution.go @@ -1,10 +1,10 @@ -func differenceOfSums(n int, m int) (ans int) { - for i := 1; i <= n; i++ { - if i%m == 0 { - ans -= i - } else { - ans += i - } - } - return +func differenceOfSums(n int, m int) (ans int) { + for i := 1; i <= n; i++ { + if i%m == 0 { + ans -= i + } else { + ans += i + } + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2895.Minimum Processing Time/Solution.go b/solution/2800-2899/2895.Minimum Processing Time/Solution.go index 17ffc7e631883..08d99a8949cfe 100644 --- a/solution/2800-2899/2895.Minimum Processing Time/Solution.go +++ b/solution/2800-2899/2895.Minimum Processing Time/Solution.go @@ -1,10 +1,10 @@ -func minProcessingTime(processorTime []int, tasks []int) (ans int) { - sort.Ints(processorTime) - sort.Ints(tasks) - i := len(tasks) - 1 - for _, t := range processorTime { - ans = max(ans, t+tasks[i]) - i -= 4 - } - return +func minProcessingTime(processorTime []int, tasks []int) (ans int) { + sort.Ints(processorTime) + sort.Ints(tasks) + i := len(tasks) - 1 + for _, t := range processorTime { + ans = max(ans, t+tasks[i]) + i -= 4 + } + return } \ No newline at end of file diff --git a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/Solution.go b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/Solution.go index 955c2b94065c1..914d75bcb25a1 100644 --- a/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/Solution.go +++ b/solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/Solution.go @@ -1,33 +1,33 @@ -func minOperations(s1 string, s2 string, x int) int { - idx := []int{} - for i := range s1 { - if s1[i] != s2[i] { - idx = append(idx, i) - } - } - m := len(idx) - if m&1 == 1 { - return -1 - } - f := make([][]int, m) - for i := range f { - f[i] = make([]int, m) - for j := range f[i] { - f[i][j] = -1 - } - } - var dfs func(i, j int) int - dfs = func(i, j int) int { - if i > j { - return 0 - } - if f[i][j] != -1 { - return f[i][j] - } - f[i][j] = dfs(i+1, j-1) + x - f[i][j] = min(f[i][j], dfs(i+2, j)+idx[i+1]-idx[i]) - f[i][j] = min(f[i][j], dfs(i, j-2)+idx[j]-idx[j-1]) - return f[i][j] - } - return dfs(0, m-1) +func minOperations(s1 string, s2 string, x int) int { + idx := []int{} + for i := range s1 { + if s1[i] != s2[i] { + idx = append(idx, i) + } + } + m := len(idx) + if m&1 == 1 { + return -1 + } + f := make([][]int, m) + for i := range f { + f[i] = make([]int, m) + for j := range f[i] { + f[i][j] = -1 + } + } + var dfs func(i, j int) int + dfs = func(i, j int) int { + if i > j { + return 0 + } + if f[i][j] != -1 { + return f[i][j] + } + f[i][j] = dfs(i+1, j-1) + x + f[i][j] = min(f[i][j], dfs(i+2, j)+idx[i+1]-idx[i]) + f[i][j] = min(f[i][j], dfs(i, j-2)+idx[j]-idx[j-1]) + return f[i][j] + } + return dfs(0, m-1) } \ No newline at end of file diff --git a/solution/2800-2899/2897.Apply Operations on Array to Maximize Sum of Squares/Solution.go b/solution/2800-2899/2897.Apply Operations on Array to Maximize Sum of Squares/Solution.go index c17aae9249166..1014a23650a6d 100644 --- a/solution/2800-2899/2897.Apply Operations on Array to Maximize Sum of Squares/Solution.go +++ b/solution/2800-2899/2897.Apply Operations on Array to Maximize Sum of Squares/Solution.go @@ -1,22 +1,22 @@ -func maxSum(nums []int, k int) (ans int) { - cnt := [31]int{} - for _, x := range nums { - for i := 0; i < 31; i++ { - if x>>i&1 == 1 { - cnt[i]++ - } - } - } - const mod int = 1e9 + 7 - for ; k > 0; k-- { - x := 0 - for i := 0; i < 31; i++ { - if cnt[i] > 0 { - x |= 1 << i - cnt[i]-- - } - } - ans = (ans + x*x) % mod - } - return +func maxSum(nums []int, k int) (ans int) { + cnt := [31]int{} + for _, x := range nums { + for i := 0; i < 31; i++ { + if x>>i&1 == 1 { + cnt[i]++ + } + } + } + const mod int = 1e9 + 7 + for ; k > 0; k-- { + x := 0 + for i := 0; i < 31; i++ { + if cnt[i] > 0 { + x |= 1 << i + cnt[i]-- + } + } + ans = (ans + x*x) % mod + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go index 1725730b40191..54f7f6e5de467 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go @@ -1,45 +1,45 @@ -func getWordsInLongestSubsequence(n int, words []string, groups []int) []string { - check := func(s, t string) bool { - if len(s) != len(t) { - return false - } - cnt := 0 - for i := range s { - if s[i] != t[i] { - cnt++ - } - } - return cnt == 1 - } - f := make([]int, n) - g := make([]int, n) - for i := range f { - f[i] = 1 - g[i] = -1 - } - mx := 1 - for i, x := range groups { - for j, y := range groups[:i] { - if x != y && f[i] < f[j]+1 && check(words[i], words[j]) { - f[i] = f[j] + 1 - g[i] = j - if mx < f[i] { - mx = f[i] - } - } - } - } - ans := make([]string, 0, mx) - for i, x := range f { - if x == mx { - for j := i; j >= 0; j = g[j] { - ans = append(ans, words[j]) - } - break - } - } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } - return ans +func getWordsInLongestSubsequence(n int, words []string, groups []int) []string { + check := func(s, t string) bool { + if len(s) != len(t) { + return false + } + cnt := 0 + for i := range s { + if s[i] != t[i] { + cnt++ + } + } + return cnt == 1 + } + f := make([]int, n) + g := make([]int, n) + for i := range f { + f[i] = 1 + g[i] = -1 + } + mx := 1 + for i, x := range groups { + for j, y := range groups[:i] { + if x != y && f[i] < f[j]+1 && check(words[i], words[j]) { + f[i] = f[j] + 1 + g[i] = j + if mx < f[i] { + mx = f[i] + } + } + } + } + ans := make([]string, 0, mx) + for i, x := range f { + if x == mx { + for j := i; j >= 0; j = g[j] { + ans = append(ans, words[j]) + } + break + } + } + for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { + ans[i], ans[j] = ans[j], ans[i] + } + return ans } \ No newline at end of file diff --git a/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.go b/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.go index 51117dc3b8b61..a821ae9a388df 100644 --- a/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.go +++ b/solution/2900-2999/2903.Find Indices With Index and Value Difference I/Solution.go @@ -1,19 +1,19 @@ -func findIndices(nums []int, indexDifference int, valueDifference int) []int { - mi, mx := 0, 0 - for i := indexDifference; i < len(nums); i++ { - j := i - indexDifference - if nums[j] < nums[mi] { - mi = j - } - if nums[j] > nums[mx] { - mx = j - } - if nums[i]-nums[mi] >= valueDifference { - return []int{mi, i} - } - if nums[mx]-nums[i] >= valueDifference { - return []int{mx, i} - } - } - return []int{-1, -1} +func findIndices(nums []int, indexDifference int, valueDifference int) []int { + mi, mx := 0, 0 + for i := indexDifference; i < len(nums); i++ { + j := i - indexDifference + if nums[j] < nums[mi] { + mi = j + } + if nums[j] > nums[mx] { + mx = j + } + if nums[i]-nums[mi] >= valueDifference { + return []int{mi, i} + } + if nums[mx]-nums[i] >= valueDifference { + return []int{mx, i} + } + } + return []int{-1, -1} } \ No newline at end of file diff --git a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.go b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.go index f3738e3468691..075835b8a2b61 100644 --- a/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.go +++ b/solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/Solution.go @@ -1,17 +1,17 @@ -func shortestBeautifulSubstring(s string, k int) (ans string) { - i, j, cnt := 0, 0, 0 - n := len(s) - for j < n { - cnt += int(s[j] - '0') - for cnt > k || (i < j && s[i] == '0') { - cnt -= int(s[i] - '0') - i++ - } - j++ - t := s[i:j] - if cnt == k && (ans == "" || j-i < len(ans) || (j-i == len(ans) && t < ans)) { - ans = t - } - } - return +func shortestBeautifulSubstring(s string, k int) (ans string) { + i, j, cnt := 0, 0, 0 + n := len(s) + for j < n { + cnt += int(s[j] - '0') + for cnt > k || (i < j && s[i] == '0') { + cnt -= int(s[i] - '0') + i++ + } + j++ + t := s[i:j] + if cnt == k && (ans == "" || j-i < len(ans) || (j-i == len(ans) && t < ans)) { + ans = t + } + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.go b/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.go index 51117dc3b8b61..a821ae9a388df 100644 --- a/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.go +++ b/solution/2900-2999/2905.Find Indices With Index and Value Difference II/Solution.go @@ -1,19 +1,19 @@ -func findIndices(nums []int, indexDifference int, valueDifference int) []int { - mi, mx := 0, 0 - for i := indexDifference; i < len(nums); i++ { - j := i - indexDifference - if nums[j] < nums[mi] { - mi = j - } - if nums[j] > nums[mx] { - mx = j - } - if nums[i]-nums[mi] >= valueDifference { - return []int{mi, i} - } - if nums[mx]-nums[i] >= valueDifference { - return []int{mx, i} - } - } - return []int{-1, -1} +func findIndices(nums []int, indexDifference int, valueDifference int) []int { + mi, mx := 0, 0 + for i := indexDifference; i < len(nums); i++ { + j := i - indexDifference + if nums[j] < nums[mi] { + mi = j + } + if nums[j] > nums[mx] { + mx = j + } + if nums[i]-nums[mi] >= valueDifference { + return []int{mi, i} + } + if nums[mx]-nums[i] >= valueDifference { + return []int{mx, i} + } + } + return []int{-1, -1} } \ No newline at end of file diff --git a/solution/2900-2999/2906.Construct Product Matrix/Solution.go b/solution/2900-2999/2906.Construct Product Matrix/Solution.go index 83ff4050a9261..778827315f866 100644 --- a/solution/2900-2999/2906.Construct Product Matrix/Solution.go +++ b/solution/2900-2999/2906.Construct Product Matrix/Solution.go @@ -1,23 +1,23 @@ -func constructProductMatrix(grid [][]int) [][]int { - const mod int = 12345 - n, m := len(grid), len(grid[0]) - p := make([][]int, n) - for i := range p { - p[i] = make([]int, m) - } - suf := 1 - for i := n - 1; i >= 0; i-- { - for j := m - 1; j >= 0; j-- { - p[i][j] = suf - suf = suf * grid[i][j] % mod - } - } - pre := 1 - for i := 0; i < n; i++ { - for j := 0; j < m; j++ { - p[i][j] = p[i][j] * pre % mod - pre = pre * grid[i][j] % mod - } - } - return p +func constructProductMatrix(grid [][]int) [][]int { + const mod int = 12345 + n, m := len(grid), len(grid[0]) + p := make([][]int, n) + for i := range p { + p[i] = make([]int, m) + } + suf := 1 + for i := n - 1; i >= 0; i-- { + for j := m - 1; j >= 0; j-- { + p[i][j] = suf + suf = suf * grid[i][j] % mod + } + } + pre := 1 + for i := 0; i < n; i++ { + for j := 0; j < m; j++ { + p[i][j] = p[i][j] * pre % mod + pre = pre * grid[i][j] % mod + } + } + return p } \ No newline at end of file diff --git a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.go b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.go index 7dd95e7b9f7b0..c7bb57a9813fe 100644 --- a/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.go +++ b/solution/2900-2999/2907.Maximum Profitable Triplets With Increasing Prices I/Solution.go @@ -1,21 +1,21 @@ -func maxProfit(prices []int, profits []int) int { - n := len(prices) - ans := -1 - for j, x := range profits { - left, right := 0, 0 - for i := 0; i < j; i++ { - if prices[i] < prices[j] { - left = max(left, profits[i]) - } - } - for k := j + 1; k < n; k++ { - if prices[j] < prices[k] { - right = max(right, profits[k]) - } - } - if left > 0 && right > 0 { - ans = max(ans, left+x+right) - } - } - return ans +func maxProfit(prices []int, profits []int) int { + n := len(prices) + ans := -1 + for j, x := range profits { + left, right := 0, 0 + for i := 0; i < j; i++ { + if prices[i] < prices[j] { + left = max(left, profits[i]) + } + } + for k := j + 1; k < n; k++ { + if prices[j] < prices[k] { + right = max(right, profits[k]) + } + } + if left > 0 && right > 0 { + ans = max(ans, left+x+right) + } + } + return ans } \ No newline at end of file diff --git a/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.go b/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.go index d633c5b1dc40d..6a0061be768f4 100644 --- a/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.go +++ b/solution/2900-2999/2910.Minimum Number of Groups to Create a Valid Assignment/Solution.go @@ -1,23 +1,23 @@ -func minGroupsForValidAssignment(nums []int) int { - cnt := map[int]int{} - for _, x := range nums { - cnt[x]++ - } - k := len(nums) - for _, v := range cnt { - k = min(k, v) - } - for ; ; k-- { - ans := 0 - for _, v := range cnt { - if v/k < v%k { - ans = 0 - break - } - ans += (v + k) / (k + 1) - } - if ans > 0 { - return ans - } - } +func minGroupsForValidAssignment(nums []int) int { + cnt := map[int]int{} + for _, x := range nums { + cnt[x]++ + } + k := len(nums) + for _, v := range cnt { + k = min(k, v) + } + for ; ; k-- { + ans := 0 + for _, v := range cnt { + if v/k < v%k { + ans = 0 + break + } + ans += (v + k) / (k + 1) + } + if ans > 0 { + return ans + } + } } \ No newline at end of file diff --git a/solution/2900-2999/2911.Minimum Changes to Make K Semi-palindromes/Solution.go b/solution/2900-2999/2911.Minimum Changes to Make K Semi-palindromes/Solution.go index 68510438de582..6f2aa02f1e8b5 100644 --- a/solution/2900-2999/2911.Minimum Changes to Make K Semi-palindromes/Solution.go +++ b/solution/2900-2999/2911.Minimum Changes to Make K Semi-palindromes/Solution.go @@ -1,45 +1,45 @@ -func minimumChanges(s string, k int) int { - n := len(s) - g := make([][]int, n+1) - f := make([][]int, n+1) - const inf int = 1 << 30 - for i := range g { - g[i] = make([]int, n+1) - f[i] = make([]int, k+1) - for j := range g[i] { - g[i][j] = inf - } - for j := range f[i] { - f[i][j] = inf - } - } - f[0][0] = 0 - for i := 1; i <= n; i++ { - for j := i; j <= n; j++ { - m := j - i + 1 - for d := 1; d < m; d++ { - if m%d == 0 { - cnt := 0 - for l := 0; l < m; l++ { - r := (m/d-1-l/d)*d + l%d - if l >= r { - break - } - if s[i-1+l] != s[i-1+r] { - cnt++ - } - } - g[i][j] = min(g[i][j], cnt) - } - } - } - } - for i := 1; i <= n; i++ { - for j := 1; j <= k; j++ { - for h := 0; h < i-1; h++ { - f[i][j] = min(f[i][j], f[h][j-1]+g[h+1][i]) - } - } - } - return f[n][k] +func minimumChanges(s string, k int) int { + n := len(s) + g := make([][]int, n+1) + f := make([][]int, n+1) + const inf int = 1 << 30 + for i := range g { + g[i] = make([]int, n+1) + f[i] = make([]int, k+1) + for j := range g[i] { + g[i][j] = inf + } + for j := range f[i] { + f[i][j] = inf + } + } + f[0][0] = 0 + for i := 1; i <= n; i++ { + for j := i; j <= n; j++ { + m := j - i + 1 + for d := 1; d < m; d++ { + if m%d == 0 { + cnt := 0 + for l := 0; l < m; l++ { + r := (m/d-1-l/d)*d + l%d + if l >= r { + break + } + if s[i-1+l] != s[i-1+r] { + cnt++ + } + } + g[i][j] = min(g[i][j], cnt) + } + } + } + } + for i := 1; i <= n; i++ { + for j := 1; j <= k; j++ { + for h := 0; h < i-1; h++ { + f[i][j] = min(f[i][j], f[h][j-1]+g[h+1][i]) + } + } + } + return f[n][k] } \ No newline at end of file diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go index 263dc63058cc8..92f11592b39c4 100644 --- a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go @@ -1,14 +1,14 @@ -func sumCounts(nums []int) (ans int) { - for i := range nums { - s := [101]int{} - cnt := 0 - for _, x := range nums[i:] { - s[x]++ - if s[x] == 1 { - cnt++ - } - ans += cnt * cnt - } - } - return +func sumCounts(nums []int) (ans int) { + for i := range nums { + s := [101]int{} + cnt := 0 + for _, x := range nums[i:] { + s[x]++ + if s[x] == 1 { + cnt++ + } + ans += cnt * cnt + } + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go index 2e5eff143b5fb..6189f370f2eaf 100644 --- a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go @@ -1,8 +1,8 @@ -func minChanges(s string) (ans int) { - for i := 1; i < len(s); i += 2 { - if s[i] != s[i-1] { - ans++ - } - } - return +func minChanges(s string) (ans int) { + for i := 1; i < len(s); i += 2 { + if s[i] != s[i-1] { + ans++ + } + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2915.Length of the Longest Subsequence That Sums to Target/Solution.go b/solution/2900-2999/2915.Length of the Longest Subsequence That Sums to Target/Solution.go index 47488b4dbd5ef..ef03163e6ec18 100644 --- a/solution/2900-2999/2915.Length of the Longest Subsequence That Sums to Target/Solution.go +++ b/solution/2900-2999/2915.Length of the Longest Subsequence That Sums to Target/Solution.go @@ -1,16 +1,16 @@ -func lengthOfLongestSubsequence(nums []int, target int) int { - f := make([]int, target+1) - for i := range f { - f[i] = -(1 << 30) - } - f[0] = 0 - for _, x := range nums { - for j := target; j >= x; j-- { - f[j] = max(f[j], f[j-x]+1) - } - } - if f[target] <= 0 { - return -1 - } - return f[target] +func lengthOfLongestSubsequence(nums []int, target int) int { + f := make([]int, target+1) + for i := range f { + f[i] = -(1 << 30) + } + f[0] = 0 + for _, x := range nums { + for j := target; j >= x; j-- { + f[j] = max(f[j], f[j-x]+1) + } + } + if f[target] <= 0 { + return -1 + } + return f[target] } \ No newline at end of file diff --git a/solution/2900-2999/2917.Find the K-or of an Array/Solution.go b/solution/2900-2999/2917.Find the K-or of an Array/Solution.go index 6b4349e10a895..3c4d0286aa7e3 100644 --- a/solution/2900-2999/2917.Find the K-or of an Array/Solution.go +++ b/solution/2900-2999/2917.Find the K-or of an Array/Solution.go @@ -1,12 +1,12 @@ -func findKOr(nums []int, k int) (ans int) { - for i := 0; i < 32; i++ { - cnt := 0 - for _, x := range nums { - cnt += (x >> i & 1) - } - if cnt >= k { - ans |= 1 << i - } - } - return +func findKOr(nums []int, k int) (ans int) { + for i := 0; i < 32; i++ { + cnt := 0 + for _, x := range nums { + cnt += (x >> i & 1) + } + if cnt >= k { + ans |= 1 << i + } + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/Solution.go b/solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/Solution.go index 676f4c8d5245a..1d48ba508cc25 100644 --- a/solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/Solution.go +++ b/solution/2900-2999/2918.Minimum Equal Sum of Two Arrays After Replacing Zeros/Solution.go @@ -1,23 +1,23 @@ -func minSum(nums1 []int, nums2 []int) int64 { - s1, s2 := 0, 0 - hasZero := false - for _, x := range nums1 { - if x == 0 { - hasZero = true - } - s1 += max(x, 1) - } - for _, x := range nums2 { - s2 += max(x, 1) - } - if s1 > s2 { - return minSum(nums2, nums1) - } - if s1 == s2 { - return int64(s1) - } - if hasZero { - return int64(s2) - } - return -1 +func minSum(nums1 []int, nums2 []int) int64 { + s1, s2 := 0, 0 + hasZero := false + for _, x := range nums1 { + if x == 0 { + hasZero = true + } + s1 += max(x, 1) + } + for _, x := range nums2 { + s2 += max(x, 1) + } + if s1 > s2 { + return minSum(nums2, nums1) + } + if s1 == s2 { + return int64(s1) + } + if hasZero { + return int64(s2) + } + return -1 } \ No newline at end of file diff --git a/solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/Solution.go b/solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/Solution.go index 7b6eeae2950e2..789cd5c054cab 100644 --- a/solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/Solution.go +++ b/solution/2900-2999/2919.Minimum Increment Operations to Make Array Beautiful/Solution.go @@ -1,7 +1,7 @@ -func minIncrementOperations(nums []int, k int) int64 { - var f, g, h int - for _, x := range nums { - f, g, h = g, h, min(f, g, h)+max(k-x, 0) - } - return int64(min(f, g, h)) +func minIncrementOperations(nums []int, k int) int64 { + var f, g, h int + for _, x := range nums { + f, g, h = g, h, min(f, g, h)+max(k-x, 0) + } + return int64(min(f, g, h)) } \ No newline at end of file diff --git a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.go b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.go index fb35aff995139..de11d48fdebb8 100644 --- a/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.go +++ b/solution/2900-2999/2921.Maximum Profitable Triplets With Increasing Prices II/Solution.go @@ -1,60 +1,60 @@ -type BinaryIndexedTree struct { - n int - c []int -} - -func NewBinaryIndexedTree(n int) BinaryIndexedTree { - c := make([]int, n+1) - return BinaryIndexedTree{n: n, c: c} -} - -func (bit *BinaryIndexedTree) update(x, v int) { - for x <= bit.n { - bit.c[x] = max(bit.c[x], v) - x += x & -x - } -} - -func (bit *BinaryIndexedTree) query(x int) int { - mx := 0 - for x > 0 { - mx = max(mx, bit.c[x]) - x -= x & -x - } - return mx -} - -func maxProfit(prices []int, profits []int) int { - n := len(prices) - left := make([]int, n) - right := make([]int, n) - m := 0 - - for _, x := range prices { - m = max(m, x) - } - - tree1 := NewBinaryIndexedTree(m + 1) - tree2 := NewBinaryIndexedTree(m + 1) - - for i, x := range prices { - left[i] = tree1.query(x - 1) - tree1.update(x, profits[i]) - } - - for i := n - 1; i >= 0; i-- { - x := m + 1 - prices[i] - right[i] = tree2.query(x - 1) - tree2.update(x, profits[i]) - } - - ans := -1 - - for i := 0; i < n; i++ { - if left[i] > 0 && right[i] > 0 { - ans = max(ans, left[i]+profits[i]+right[i]) - } - } - - return ans +type BinaryIndexedTree struct { + n int + c []int +} + +func NewBinaryIndexedTree(n int) BinaryIndexedTree { + c := make([]int, n+1) + return BinaryIndexedTree{n: n, c: c} +} + +func (bit *BinaryIndexedTree) update(x, v int) { + for x <= bit.n { + bit.c[x] = max(bit.c[x], v) + x += x & -x + } +} + +func (bit *BinaryIndexedTree) query(x int) int { + mx := 0 + for x > 0 { + mx = max(mx, bit.c[x]) + x -= x & -x + } + return mx +} + +func maxProfit(prices []int, profits []int) int { + n := len(prices) + left := make([]int, n) + right := make([]int, n) + m := 0 + + for _, x := range prices { + m = max(m, x) + } + + tree1 := NewBinaryIndexedTree(m + 1) + tree2 := NewBinaryIndexedTree(m + 1) + + for i, x := range prices { + left[i] = tree1.query(x - 1) + tree1.update(x, profits[i]) + } + + for i := n - 1; i >= 0; i-- { + x := m + 1 - prices[i] + right[i] = tree2.query(x - 1) + tree2.update(x, profits[i]) + } + + ans := -1 + + for i := 0; i < n; i++ { + if left[i] > 0 && right[i] > 0 { + ans = max(ans, left[i]+profits[i]+right[i]) + } + } + + return ans } \ No newline at end of file diff --git a/solution/2900-2999/2923.Find Champion I/Solution.go b/solution/2900-2999/2923.Find Champion I/Solution.go index f779cf06861d4..73a9a77ad9398 100644 --- a/solution/2900-2999/2923.Find Champion I/Solution.go +++ b/solution/2900-2999/2923.Find Champion I/Solution.go @@ -1,14 +1,14 @@ -func findChampion(grid [][]int) int { - n := len(grid) - for i := 0; ; i++ { - cnt := 0 - for j, x := range grid[i] { - if i != j && x == 1 { - cnt++ - } - } - if cnt == n-1 { - return i - } - } +func findChampion(grid [][]int) int { + n := len(grid) + for i := 0; ; i++ { + cnt := 0 + for j, x := range grid[i] { + if i != j && x == 1 { + cnt++ + } + } + if cnt == n-1 { + return i + } + } } \ No newline at end of file diff --git a/solution/2900-2999/2924.Find Champion II/Solution.go b/solution/2900-2999/2924.Find Champion II/Solution.go index bf72196deba33..91e3cb75e8857 100644 --- a/solution/2900-2999/2924.Find Champion II/Solution.go +++ b/solution/2900-2999/2924.Find Champion II/Solution.go @@ -1,17 +1,17 @@ -func findChampion(n int, edges [][]int) int { - indeg := make([]int, n) - for _, e := range edges { - indeg[e[1]]++ - } - ans, cnt := -1, 0 - for i, x := range indeg { - if x == 0 { - cnt++ - ans = i - } - } - if cnt == 1 { - return ans - } - return -1 +func findChampion(n int, edges [][]int) int { + indeg := make([]int, n) + for _, e := range edges { + indeg[e[1]]++ + } + ans, cnt := -1, 0 + for i, x := range indeg { + if x == 0 { + cnt++ + ans = i + } + } + if cnt == 1 { + return ans + } + return -1 } \ No newline at end of file