diff --git "a/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/README.md" "b/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/README.md" index 683c73c8e8a73..d61bf5010066b 100644 --- "a/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/README.md" +++ "b/lcp/LCP 22. \351\273\221\347\231\275\346\226\271\346\240\274\347\224\273/README.md" @@ -203,7 +203,7 @@ class Solution { if k == 0 || k == n * n { return 1 } - + func combination(_ n: Int, _ r: Int) -> Int { guard r <= n else { return 0 } if r == 0 || r == n { return 1 } @@ -213,9 +213,9 @@ class Solution { } return result } - + var ans = 0 - + for i in 0...n { for j in 0...n { let paintedCells = n * (i + j) - i * j @@ -224,7 +224,7 @@ class Solution { } } } - + return ans } } diff --git "a/lcp/LCP 24. \346\225\260\345\255\227\346\270\270\346\210\217/README.md" "b/lcp/LCP 24. \346\225\260\345\255\227\346\270\270\346\210\217/README.md" index 986797a81ac1d..a53ff9a0a21f1 100644 --- "a/lcp/LCP 24. \346\225\260\345\255\227\346\270\270\346\210\217/README.md" +++ "b/lcp/LCP 24. \346\225\260\345\255\227\346\270\270\346\210\217/README.md" @@ -320,12 +320,12 @@ class MedianFinder { init() {} func addNum(_ num: Int) { - + upperHeap.append(num) sumUpper += num upperHeap.sort() - + if let minUpper = upperHeap.first { upperHeap.removeFirst() lowerHeap.append(minUpper) @@ -334,7 +334,7 @@ class MedianFinder { lowerHeap.sort(by: >) } - + if lowerHeap.count > upperHeap.count + 1 { if let maxLower = lowerHeap.first { lowerHeap.removeFirst() diff --git "a/lcp/LCP 25. \345\217\244\350\221\243\351\224\256\347\233\230/README.md" "b/lcp/LCP 25. \345\217\244\350\221\243\351\224\256\347\233\230/README.md" index 4b74397330c70..e7790f0ff417a 100644 --- "a/lcp/LCP 25. \345\217\244\350\221\243\351\224\256\347\233\230/README.md" +++ "b/lcp/LCP 25. \345\217\244\350\221\243\351\224\256\347\233\230/README.md" @@ -203,18 +203,18 @@ class Solution { for i in 0...n { c[i][0] = 1 } - + for i in 1...n { for j in 1...k { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod } } - + var f = Array(repeating: Array(repeating: 0, count: 27), count: n + 1) for j in 0..<27 { f[0][j] = 1 } - + for i in 1...n { for j in 1..<27 { for h in 0...min(i, k) { @@ -222,7 +222,7 @@ class Solution { } } } - + return f[n][26] } } diff --git a/solution/0800-0899/0867.Transpose Matrix/README.md b/solution/0800-0899/0867.Transpose Matrix/README.md index eb83135b82ce1..56af8d52ea03b 100644 --- a/solution/0800-0899/0867.Transpose Matrix/README.md +++ b/solution/0800-0899/0867.Transpose Matrix/README.md @@ -58,7 +58,15 @@ tags: -### 方法一 +### 方法一:模拟 + +我们记矩阵 $\textit{matrix}$ 的行数为 $m$,列数为 $n$。根据转置的定义,转置后的矩阵 $\textit{ans}$ 的行数为 $n$,列数为 $m$。 + +对于 $\textit{ans}$ 中的任意位置 $(i,j)$,其对应于矩阵 $\textit{matrix}$ 中的位置 $(j,i)$。因此,我们遍历矩阵 $\textit{matrix}$ 中的每个元素,将其转置到 $\textit{ans}$ 中相应的位置。 + +遍历结束后,返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵 $\textit{matrix}$ 的行数和列数。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -95,9 +103,11 @@ public: vector> transpose(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector> ans(n, vector(m)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < m; ++j) + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; + } + } return ans; } }; @@ -119,6 +129,21 @@ func transpose(matrix [][]int) [][]int { } ``` +#### TypeScript + +```ts +function transpose(matrix: number[][]): number[][] { + const [m, n] = [matrix.length, matrix[0].length]; + const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + ans[i][j] = matrix[j][i]; + } + } + return ans; +} +``` + #### JavaScript ```js @@ -127,9 +152,8 @@ func transpose(matrix [][]int) [][]int { * @return {number[][]} */ var transpose = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - const ans = new Array(n).fill(0).map(() => new Array(m).fill(0)); + const [m, n] = [matrix.length, matrix[0].length]; + const ans = Array.from({ length: n }, () => Array(m).fill(0)); for (let i = 0; i < n; ++i) { for (let j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; diff --git a/solution/0800-0899/0867.Transpose Matrix/README_EN.md b/solution/0800-0899/0867.Transpose Matrix/README_EN.md index 1d26e15fbbb56..3e5136d6d6af3 100644 --- a/solution/0800-0899/0867.Transpose Matrix/README_EN.md +++ b/solution/0800-0899/0867.Transpose Matrix/README_EN.md @@ -56,7 +56,15 @@ tags: -### Solution 1 +### Solution 1: Simulation + +Let $m$ be the number of rows and $n$ be the number of columns in the matrix $\textit{matrix}$. According to the definition of transpose, the transposed matrix $\textit{ans}$ will have $n$ rows and $m$ columns. + +For any position $(i, j)$ in $\textit{ans}$, it corresponds to the position $(j, i)$ in the matrix $\textit{matrix}$. Therefore, we traverse each element in the matrix $\textit{matrix}$ and transpose it to the corresponding position in $\textit{ans}$. + +After the traversal, we return $\textit{ans}$. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix $\textit{matrix}$, respectively. Ignoring the space consumption of the answer, the space complexity is $O(1)$. @@ -93,9 +101,11 @@ public: vector> transpose(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector> ans(n, vector(m)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < m; ++j) + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; + } + } return ans; } }; @@ -117,6 +127,21 @@ func transpose(matrix [][]int) [][]int { } ``` +#### TypeScript + +```ts +function transpose(matrix: number[][]): number[][] { + const [m, n] = [matrix.length, matrix[0].length]; + const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + ans[i][j] = matrix[j][i]; + } + } + return ans; +} +``` + #### JavaScript ```js @@ -125,9 +150,8 @@ func transpose(matrix [][]int) [][]int { * @return {number[][]} */ var transpose = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - const ans = new Array(n).fill(0).map(() => new Array(m).fill(0)); + const [m, n] = [matrix.length, matrix[0].length]; + const ans = Array.from({ length: n }, () => Array(m).fill(0)); for (let i = 0; i < n; ++i) { for (let j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; diff --git a/solution/0800-0899/0867.Transpose Matrix/Solution.cpp b/solution/0800-0899/0867.Transpose Matrix/Solution.cpp index c3ec4c81c1568..934aa33089589 100644 --- a/solution/0800-0899/0867.Transpose Matrix/Solution.cpp +++ b/solution/0800-0899/0867.Transpose Matrix/Solution.cpp @@ -3,9 +3,11 @@ class Solution { vector> transpose(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector> ans(n, vector(m)); - for (int i = 0; i < n; ++i) - for (int j = 0; j < m; ++j) + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; + } + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0800-0899/0867.Transpose Matrix/Solution.js b/solution/0800-0899/0867.Transpose Matrix/Solution.js index d9d26a22e4048..89db3ce4d3836 100644 --- a/solution/0800-0899/0867.Transpose Matrix/Solution.js +++ b/solution/0800-0899/0867.Transpose Matrix/Solution.js @@ -3,9 +3,8 @@ * @return {number[][]} */ var transpose = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; - const ans = new Array(n).fill(0).map(() => new Array(m).fill(0)); + const [m, n] = [matrix.length, matrix[0].length]; + const ans = Array.from({ length: n }, () => Array(m).fill(0)); for (let i = 0; i < n; ++i) { for (let j = 0; j < m; ++j) { ans[i][j] = matrix[j][i]; diff --git a/solution/0800-0899/0867.Transpose Matrix/Solution.ts b/solution/0800-0899/0867.Transpose Matrix/Solution.ts new file mode 100644 index 0000000000000..8a194d68e6ded --- /dev/null +++ b/solution/0800-0899/0867.Transpose Matrix/Solution.ts @@ -0,0 +1,10 @@ +function transpose(matrix: number[][]): number[][] { + const [m, n] = [matrix.length, matrix[0].length]; + const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < m; ++j) { + ans[i][j] = matrix[j][i]; + } + } + return ans; +} diff --git a/solution/0800-0899/0868.Binary Gap/README.md b/solution/0800-0899/0868.Binary Gap/README.md index fd7bd56a9c427..ede43833eeeba 100644 --- a/solution/0800-0899/0868.Binary Gap/README.md +++ b/solution/0800-0899/0868.Binary Gap/README.md @@ -68,7 +68,11 @@ tags: -### 方法一 +### 方法一:位运算 + +我们用两个指针 $\textit{pre}$ 和 $\textit{cur}$ 分别表示上一个和当前的 $1$ 的位置,初始时 $\textit{pre} = 100$, $\textit{cur} = 0$,然后遍历 $n$ 的二进制表示,当遇到 $1$ 时,计算当前位置和上一个 $1$ 的位置之间的距离,并更新答案。 + +时间复杂度 $O(\log n)$,其中 $n$ 是题目给定的整数。空间复杂度 $O(1)$。 @@ -77,12 +81,13 @@ tags: ```python class Solution: def binaryGap(self, n: int) -> int: - ans, j = 0, -1 - for i in range(32): + ans = 0 + pre, cur = inf, 0 + while n: if n & 1: - if j != -1: - ans = max(ans, i - j) - j = i + ans = max(ans, cur - pre) + pre = cur + cur += 1 n >>= 1 return ans ``` @@ -93,13 +98,12 @@ class Solution: class Solution { public int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n != 0; ++i, n >>= 1) { - if ((n & 1) == 1) { - if (j != -1) { - ans = Math.max(ans, i - j); - } - j = i; + for (int pre = 100, cur = 0; n != 0; n >>= 1) { + if (n % 2 == 1) { + ans = Math.max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } @@ -113,11 +117,12 @@ class Solution { public: int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n; ++i, n >>= 1) { + for (int pre = 100, cur = 0; n != 0; n >>= 1) { if (n & 1) { - if (j != -1) ans = max(ans, i - j); - j = i; + ans = max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } @@ -127,17 +132,15 @@ public: #### Go ```go -func binaryGap(n int) int { - ans := 0 - for i, j := 0, -1; n != 0; i, n = i+1, n>>1 { - if (n & 1) == 1 { - if j != -1 && ans < i-j { - ans = i - j - } - j = i +func binaryGap(n int) (ans int) { + for pre, cur := 100, 0; n != 0; n >>= 1 { + if n&1 == 1 { + ans = max(ans, cur-pre) + pre = cur } + cur++ } - return ans + return } ``` @@ -145,18 +148,15 @@ func binaryGap(n int) int { ```ts function binaryGap(n: number): number { - let res = 0; - let j = -1; - for (let i = 0; n !== 0; i++) { + let ans = 0; + for (let pre = 100, cur = 0; n; n >>= 1) { if (n & 1) { - if (j !== -1) { - res = Math.max(res, i - j); - } - j = i; + ans = Math.max(ans, cur - pre); + pre = cur; } - n >>= 1; + ++cur; } - return res; + return ans; } ``` @@ -165,20 +165,18 @@ function binaryGap(n: number): number { ```rust impl Solution { pub fn binary_gap(mut n: i32) -> i32 { - let mut res = 0; - let mut i = 0; - let mut j = -1; + let mut ans = 0; + let mut pre = 100; + let mut cur = 0; while n != 0 { - if (n & 1) == 1 { - if j != -1 { - res = res.max(i - j); - } - j = i; + if n % 2 == 1 { + ans = ans.max(cur - pre); + pre = cur; } + cur += 1; n >>= 1; - i += 1; } - res + ans } } ``` diff --git a/solution/0800-0899/0868.Binary Gap/README_EN.md b/solution/0800-0899/0868.Binary Gap/README_EN.md index 6032762513783..13c47539ee9a3 100644 --- a/solution/0800-0899/0868.Binary Gap/README_EN.md +++ b/solution/0800-0899/0868.Binary Gap/README_EN.md @@ -63,7 +63,11 @@ There are not any adjacent pairs of 1's in the binary representation of 8, s -### Solution 1 +### Solution 1: Bit Manipulation + +We use two pointers $\textit{pre}$ and $\textit{cur}$ to represent the positions of the previous and current $1$ bits, respectively. Initially, $\textit{pre} = 100$ and $\textit{cur} = 0$. Then, we traverse the binary representation of $n$. When we encounter a $1$, we calculate the distance between the current position and the previous $1$ position and update the answer. + +The time complexity is $O(\log n)$, where $n$ is the given integer. The space complexity is $O(1)$. @@ -72,12 +76,13 @@ There are not any adjacent pairs of 1's in the binary representation of 8, s ```python class Solution: def binaryGap(self, n: int) -> int: - ans, j = 0, -1 - for i in range(32): + ans = 0 + pre, cur = inf, 0 + while n: if n & 1: - if j != -1: - ans = max(ans, i - j) - j = i + ans = max(ans, cur - pre) + pre = cur + cur += 1 n >>= 1 return ans ``` @@ -88,13 +93,12 @@ class Solution: class Solution { public int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n != 0; ++i, n >>= 1) { - if ((n & 1) == 1) { - if (j != -1) { - ans = Math.max(ans, i - j); - } - j = i; + for (int pre = 100, cur = 0; n != 0; n >>= 1) { + if (n % 2 == 1) { + ans = Math.max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } @@ -108,11 +112,12 @@ class Solution { public: int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n; ++i, n >>= 1) { + for (int pre = 100, cur = 0; n != 0; n >>= 1) { if (n & 1) { - if (j != -1) ans = max(ans, i - j); - j = i; + ans = max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } @@ -122,17 +127,15 @@ public: #### Go ```go -func binaryGap(n int) int { - ans := 0 - for i, j := 0, -1; n != 0; i, n = i+1, n>>1 { - if (n & 1) == 1 { - if j != -1 && ans < i-j { - ans = i - j - } - j = i +func binaryGap(n int) (ans int) { + for pre, cur := 100, 0; n != 0; n >>= 1 { + if n&1 == 1 { + ans = max(ans, cur-pre) + pre = cur } + cur++ } - return ans + return } ``` @@ -140,18 +143,15 @@ func binaryGap(n int) int { ```ts function binaryGap(n: number): number { - let res = 0; - let j = -1; - for (let i = 0; n !== 0; i++) { + let ans = 0; + for (let pre = 100, cur = 0; n; n >>= 1) { if (n & 1) { - if (j !== -1) { - res = Math.max(res, i - j); - } - j = i; + ans = Math.max(ans, cur - pre); + pre = cur; } - n >>= 1; + ++cur; } - return res; + return ans; } ``` @@ -160,20 +160,18 @@ function binaryGap(n: number): number { ```rust impl Solution { pub fn binary_gap(mut n: i32) -> i32 { - let mut res = 0; - let mut i = 0; - let mut j = -1; + let mut ans = 0; + let mut pre = 100; + let mut cur = 0; while n != 0 { - if (n & 1) == 1 { - if j != -1 { - res = res.max(i - j); - } - j = i; + if n % 2 == 1 { + ans = ans.max(cur - pre); + pre = cur; } + cur += 1; n >>= 1; - i += 1; } - res + ans } } ``` diff --git a/solution/0800-0899/0868.Binary Gap/Solution.cpp b/solution/0800-0899/0868.Binary Gap/Solution.cpp index 5e1e8885e5f5d..ddb248112bd25 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.cpp +++ b/solution/0800-0899/0868.Binary Gap/Solution.cpp @@ -2,12 +2,13 @@ class Solution { public: int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n; ++i, n >>= 1) { + for (int pre = 100, cur = 0; n != 0; n >>= 1) { if (n & 1) { - if (j != -1) ans = max(ans, i - j); - j = i; + ans = max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0800-0899/0868.Binary Gap/Solution.go b/solution/0800-0899/0868.Binary Gap/Solution.go index 3ce1ca09ae51b..2659c0573a7d3 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.go +++ b/solution/0800-0899/0868.Binary Gap/Solution.go @@ -1,12 +1,10 @@ -func binaryGap(n int) int { - ans := 0 - for i, j := 0, -1; n != 0; i, n = i+1, n>>1 { - if (n & 1) == 1 { - if j != -1 && ans < i-j { - ans = i - j - } - j = i +func binaryGap(n int) (ans int) { + for pre, cur := 100, 0; n != 0; n >>= 1 { + if n&1 == 1 { + ans = max(ans, cur-pre) + pre = cur } + cur++ } - return ans -} \ No newline at end of file + return +} diff --git a/solution/0800-0899/0868.Binary Gap/Solution.java b/solution/0800-0899/0868.Binary Gap/Solution.java index 923489a6b986d..296e4e38fe28f 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.java +++ b/solution/0800-0899/0868.Binary Gap/Solution.java @@ -1,14 +1,13 @@ class Solution { public int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n != 0; ++i, n >>= 1) { - if ((n & 1) == 1) { - if (j != -1) { - ans = Math.max(ans, i - j); - } - j = i; + for (int pre = 100, cur = 0; n != 0; n >>= 1) { + if (n % 2 == 1) { + ans = Math.max(ans, cur - pre); + pre = cur; } + ++cur; } return ans; } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0868.Binary Gap/Solution.py b/solution/0800-0899/0868.Binary Gap/Solution.py index bd3050a9b6260..fb93a3b2f411b 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.py +++ b/solution/0800-0899/0868.Binary Gap/Solution.py @@ -1,10 +1,11 @@ class Solution: def binaryGap(self, n: int) -> int: - ans, j = 0, -1 - for i in range(32): + ans = 0 + pre, cur = inf, 0 + while n: if n & 1: - if j != -1: - ans = max(ans, i - j) - j = i + ans = max(ans, cur - pre) + pre = cur + cur += 1 n >>= 1 return ans diff --git a/solution/0800-0899/0868.Binary Gap/Solution.rs b/solution/0800-0899/0868.Binary Gap/Solution.rs index 9cf793f690e83..13adfa94f9140 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.rs +++ b/solution/0800-0899/0868.Binary Gap/Solution.rs @@ -1,18 +1,16 @@ impl Solution { pub fn binary_gap(mut n: i32) -> i32 { - let mut res = 0; - let mut i = 0; - let mut j = -1; + let mut ans = 0; + let mut pre = 100; + let mut cur = 0; while n != 0 { - if (n & 1) == 1 { - if j != -1 { - res = res.max(i - j); - } - j = i; + if n % 2 == 1 { + ans = ans.max(cur - pre); + pre = cur; } + cur += 1; n >>= 1; - i += 1; } - res + ans } } diff --git a/solution/0800-0899/0868.Binary Gap/Solution.ts b/solution/0800-0899/0868.Binary Gap/Solution.ts index 2da8737a4a0ee..c30bcfc80488c 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.ts +++ b/solution/0800-0899/0868.Binary Gap/Solution.ts @@ -1,14 +1,11 @@ function binaryGap(n: number): number { - let res = 0; - let j = -1; - for (let i = 0; n !== 0; i++) { + let ans = 0; + for (let pre = 100, cur = 0; n; n >>= 1) { if (n & 1) { - if (j !== -1) { - res = Math.max(res, i - j); - } - j = i; + ans = Math.max(ans, cur - pre); + pre = cur; } - n >>= 1; + ++cur; } - return res; + return ans; } diff --git a/solution/3300-3399/3353.Minimum Total Operations/README.md b/solution/3300-3399/3353.Minimum Total Operations/README.md index 9e33b636efdcc..9b0d0b92783ea 100644 --- a/solution/3300-3399/3353.Minimum Total Operations/README.md +++ b/solution/3300-3399/3353.Minimum Total Operations/README.md @@ -2,11 +2,13 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3353.Minimum%20Total%20Operations/README.md +tags: + - 数组 --- -# [3353. Minimum Total Operations 🔒](https://leetcode.cn/problems/minimum-total-operations) +# [3353. 最小总操作数 🔒](https://leetcode.cn/problems/minimum-total-operations) [English Version](/solution/3300-3399/3353.Minimum%20Total%20Operations/README_EN.md) @@ -14,52 +16,54 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3353.Mi -

Given an array of integers nums, you can perform any number of operations on this array.

+

给定一个整数数组 nums,你可以在这个数组上进行 任意 次操作。

-

In each operation, you can:

+

在每次 操作 中,你可以:

    -
  • Choose a prefix of the array.
  • -
  • Choose an integer k (which can be negative) and add k to each element in the chosen prefix.
  • +
  • 选择这个数组的一个 前缀
  • +
  • 选择一个整数 k(可以为负)并且对选中前缀的每一个元素加 k
-

A prefix of an array is a subarray that starts from the beginning of the array and extends to any point within it.

+

数组的 前缀 是一个子数组,从数组的开头开始并延伸到数组内的任何位置。

-

Return the minimum number of operations required to make all elements in arr equal.

+

返回使 arr 中的所有元素都相等的 最小 操作数。

 

-

Example 1:

+ +

示例 1:

-

Input: nums = [1,4,2]

+

输入:nums = [1,4,2]

-

Output: 2

+

输出:2

-

Explanation:

+

解释:

    -
  • Operation 1: Choose the prefix [1, 4] of length 2 and add -2 to each element of the prefix. The array becomes [-1, 2, 2].
  • -
  • Operation 2: Choose the prefix [-1] of length 1 and add 3 to it. The array becomes [2, 2, 2].
  • -
  • Thus, the minimum number of required operations is 2.
  • +
  • 操作 1:选择长度为 2 的前缀 [1, 4] 并且对其中的所有元素加 -2。数组变为 [-1, 2, 2]
  • +
  • 操作 2:选择长度为 1 的前缀 [-1] 并且对其中的所有元素加 3。数组变为 [2, 2, 2]
  • +
  • 因此,所需的最小操作数为 2。
-

Example 2:

+

示例 2:

-

Input: nums = [10,10,10]

+

输入:nums = [10,10,10]

-

Output: 0

+

输出:0

-

Explanation:

+

解释:

    -
  • All elements are already equal, so no operations are needed.
  • +
  • 所有元素已经相等,所以不需要操作。

 

-

Constraints:

+ +

提示:

  • 1 <= nums.length <= 105
  • diff --git a/solution/3300-3399/3353.Minimum Total Operations/README_EN.md b/solution/3300-3399/3353.Minimum Total Operations/README_EN.md index f872ebdf535b0..1d930dc5a18cd 100644 --- a/solution/3300-3399/3353.Minimum Total Operations/README_EN.md +++ b/solution/3300-3399/3353.Minimum Total Operations/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3353.Minimum%20Total%20Operations/README_EN.md +tags: + - Array --- diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md index 5ccce967f115f..263ad9272ba7e 100644 --- a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README.md +tags: + - 数组 + - 前缀和 + - 模拟 --- diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md index b4418dbc2e6b5..cf3caba5b1c0f 100644 --- a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README_EN.md +tags: + - Array + - Prefix Sum + - Simulation --- diff --git a/solution/3300-3399/3355.Zero Array Transformation I/README.md b/solution/3300-3399/3355.Zero Array Transformation I/README.md index a8e29d5413143..f480e91a5527e 100644 --- a/solution/3300-3399/3355.Zero Array Transformation I/README.md +++ b/solution/3300-3399/3355.Zero Array Transformation I/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README.md +tags: + - 数组 + - 前缀和 --- diff --git a/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md b/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md index c78da98514b51..d45d68923d8c5 100644 --- a/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md +++ b/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README_EN.md +tags: + - Array + - Prefix Sum --- diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README.md b/solution/3300-3399/3356.Zero Array Transformation II/README.md index f1a712d8f4e85..f26c02d4f2faf 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README.md +tags: + - 数组 + - 二分查找 + - 前缀和 --- diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md index c6b6774946493..13d611cf16d06 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README_EN.md +tags: + - Array + - Binary Search + - Prefix Sum --- diff --git a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README.md b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README.md index 1e429806e0e08..29c5386bc961d 100644 --- a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README.md +++ b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README.md +tags: + - 贪心 + - 数组 + - 二分查找 --- diff --git a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md index b10d18385169f..b3c0b48d46ff0 100644 --- a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md +++ b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README_EN.md +tags: + - Greedy + - Array + - Binary Search --- diff --git a/solution/3300-3399/3358.Books with NULL Ratings/README.md b/solution/3300-3399/3358.Books with NULL Ratings/README.md index 795611021e5d9..cd43a910242c3 100644 --- a/solution/3300-3399/3358.Books with NULL Ratings/README.md +++ b/solution/3300-3399/3358.Books with NULL Ratings/README.md @@ -8,7 +8,7 @@ tags: -# [3358. Books with NULL Ratings 🔒](https://leetcode.cn/problems/books-with-null-ratings) +# [3358. 评分为 NULL 的图书 🔒](https://leetcode.cn/problems/books-with-null-ratings) [English Version](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) @@ -16,7 +16,7 @@ tags: -

    Table: books

    +

    表:books

     +----------------+---------+
    @@ -28,24 +28,25 @@ tags:
     | published_year | int     |
     | rating         | decimal |
     +----------------+---------+
    -book_id is the unique key for this table.
    -Each row of this table contains information about a book including its unique ID, title, author, publication year, and rating.
    -rating can be NULL, indicating that the book hasn't been rated yet.
    +book_id 是这张表的唯一主键。
    +这张表的每一行包含关于一本书的唯一 ID,题目,作者,出版年份以及评分的信息。
    +评分可能为 NULL,表示这本书还没有被评分。
     
    -

    Write a solution to find all books that have not been rated yet (i.e., have a NULL rating).

    +

    编写一个解决方案来找到所有还没有被评分的图书。(即评分为 NULL

    -

    Return the result table ordered by book_id in ascending order.

    +

    返回结果表以 book_id 升序 排序。

    -

    The result format is in the following example.

    +

    结果格式如下所示。

     

    -

    Example:

    + +

    示例:

    -

    Input:

    +

    输入:

    -

    books table:

    +

    books 表:

     +---------+------------------------+------------------+----------------+--------+
    @@ -60,7 +61,7 @@ rating can be NULL, indicating that the book hasn't been rated yet.
     +---------+------------------------+------------------+----------------+--------+
     
    -

    Output:

    +

    输出:

     +---------+------------------------+------------------+----------------+
    @@ -72,14 +73,14 @@ rating can be NULL, indicating that the book hasn't been rated yet.
     +---------+------------------------+------------------+----------------+
     
    -

    Explanation:

    +

    解释:

      -
    • The books with book_id 2, 4, and 6 have NULL ratings.
    • -
    • These books are included in the result table.
    • -
    • The other books (book_id 1, 3, and 5) have ratings and are not included.
    • +
    • book_id 为 2,4,6 的书评分为 NULL。
    • +
    • 这些书被包含在结果表中。
    • +
    • 其它书(book_id 为 1,3,5)有评分并且没有被包含。
    -The result is ordered by book_id in ascending order
    +结果以 book_id 升序排序。 diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index e721733e87097..4c1546dd1fb1f 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -300,7 +300,7 @@ | 3322 | [英超积分榜排名 III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README.md) | `数据库` | 中等 | 🔒 | | 3328 | [查找每个州的城市 II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | `数据库` | 中等 | 🔒 | | 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 | -| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | | 简单 | 🔒 | +| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 4a929da110745..b52c3d1c80776 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -298,7 +298,7 @@ Press Control + F(or Command + F on | 3322 | [Premier League Table Ranking III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README_EN.md) | `Database` | Medium | 🔒 | | 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) | `Database` | Medium | 🔒 | | 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 | -| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | | Easy | 🔒 | +| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | `Database` | Easy | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 45c124ea7c2ad..72de4e9584ef8 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3363,12 +3363,12 @@ | 3350 | [检测相邻递增子数组 II](/solution/3300-3399/3350.Adjacent%20Increasing%20Subarrays%20Detection%20II/README.md) | `数组`,`二分查找` | 中等 | 第 423 场周赛 | | 3351 | [好子序列的元素之和](/solution/3300-3399/3351.Sum%20of%20Good%20Subsequences/README.md) | `数组`,`哈希表`,`动态规划` | 困难 | 第 423 场周赛 | | 3352 | [统计小于 N 的 K 可约简整数](/solution/3300-3399/3352.Count%20K-Reducible%20Numbers%20Less%20Than%20N/README.md) | `数学`,`字符串`,`动态规划`,`组合数学` | 困难 | 第 423 场周赛 | -| 3353 | [Minimum Total Operations](/solution/3300-3399/3353.Minimum%20Total%20Operations/README.md) | | 简单 | 🔒 | -| 3354 | [使数组元素等于零](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README.md) | | 简单 | 第 424 场周赛 | -| 3355 | [零数组变换 I](/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README.md) | | 中等 | 第 424 场周赛 | -| 3356 | [零数组变换 II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README.md) | | 中等 | 第 424 场周赛 | -| 3357 | [最小化相邻元素的最大差值](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README.md) | | 困难 | 第 424 场周赛 | -| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | | 简单 | 🔒 | +| 3353 | [最小总操作数](/solution/3300-3399/3353.Minimum%20Total%20Operations/README.md) | `数组` | 简单 | 🔒 | +| 3354 | [使数组元素等于零](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README.md) | `数组`,`前缀和`,`模拟` | 简单 | 第 424 场周赛 | +| 3355 | [零数组变换 I](/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README.md) | `数组`,`前缀和` | 中等 | 第 424 场周赛 | +| 3356 | [零数组变换 II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README.md) | `数组`,`二分查找`,`前缀和` | 中等 | 第 424 场周赛 | +| 3357 | [最小化相邻元素的最大差值](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README.md) | `贪心`,`数组`,`二分查找` | 困难 | 第 424 场周赛 | +| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 13bb9199973b4..a84a5c0af4f25 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3361,12 +3361,12 @@ Press Control + F(or Command + F on | 3350 | [Adjacent Increasing Subarrays Detection II](/solution/3300-3399/3350.Adjacent%20Increasing%20Subarrays%20Detection%20II/README_EN.md) | `Array`,`Binary Search` | Medium | Weekly Contest 423 | | 3351 | [Sum of Good Subsequences](/solution/3300-3399/3351.Sum%20of%20Good%20Subsequences/README_EN.md) | `Array`,`Hash Table`,`Dynamic Programming` | Hard | Weekly Contest 423 | | 3352 | [Count K-Reducible Numbers Less Than N](/solution/3300-3399/3352.Count%20K-Reducible%20Numbers%20Less%20Than%20N/README_EN.md) | `Math`,`String`,`Dynamic Programming`,`Combinatorics` | Hard | Weekly Contest 423 | -| 3353 | [Minimum Total Operations](/solution/3300-3399/3353.Minimum%20Total%20Operations/README_EN.md) | | Easy | 🔒 | -| 3354 | [Make Array Elements Equal to Zero](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README_EN.md) | | Easy | Weekly Contest 424 | -| 3355 | [Zero Array Transformation I](/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README_EN.md) | | Medium | Weekly Contest 424 | -| 3356 | [Zero Array Transformation II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README_EN.md) | | Medium | Weekly Contest 424 | -| 3357 | [Minimize the Maximum Adjacent Element Difference](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README_EN.md) | | Hard | Weekly Contest 424 | -| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | | Easy | 🔒 | +| 3353 | [Minimum Total Operations](/solution/3300-3399/3353.Minimum%20Total%20Operations/README_EN.md) | `Array` | Easy | 🔒 | +| 3354 | [Make Array Elements Equal to Zero](/solution/3300-3399/3354.Make%20Array%20Elements%20Equal%20to%20Zero/README_EN.md) | `Array`,`Prefix Sum`,`Simulation` | Easy | Weekly Contest 424 | +| 3355 | [Zero Array Transformation I](/solution/3300-3399/3355.Zero%20Array%20Transformation%20I/README_EN.md) | `Array`,`Prefix Sum` | Medium | Weekly Contest 424 | +| 3356 | [Zero Array Transformation II](/solution/3300-3399/3356.Zero%20Array%20Transformation%20II/README_EN.md) | `Array`,`Binary Search`,`Prefix Sum` | Medium | Weekly Contest 424 | +| 3357 | [Minimize the Maximum Adjacent Element Difference](/solution/3300-3399/3357.Minimize%20the%20Maximum%20Adjacent%20Element%20Difference/README_EN.md) | `Greedy`,`Array`,`Binary Search` | Hard | Weekly Contest 424 | +| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | `Database` | Easy | 🔒 | ## Copyright