diff --git a/solution/2500-2599/2519.Count the Number of K-Big Indices/README.md b/solution/2500-2599/2519.Count the Number of K-Big Indices/README.md index 830917fc8eb3b..cf9646cf26583 100644 --- a/solution/2500-2599/2519.Count the Number of K-Big Indices/README.md +++ b/solution/2500-2599/2519.Count the Number of K-Big Indices/README.md @@ -51,11 +51,11 @@ **方法一:树状数组** -维护两个树状数组,一个记录当前位置左边小于当前位置的数的个数,另一个记录当前位置右边小于当前位置的数的个数。 +我们维护两个树状数组,一个记录当前位置左边小于当前位置的数的个数,另一个记录当前位置右边小于当前位置的数的个数。 遍历数组,对于当前位置,如果左边小于当前位置的数的个数大于等于 $k$,且右边小于当前位置的数的个数大于等于 $k$,则当前位置是 $k-big$,答案加一。 -时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。 diff --git a/solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md b/solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md index 5b32f40513bf8..d3951d5768622 100644 --- a/solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md +++ b/solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md @@ -44,6 +44,14 @@ ## Solutions +**Solution 1: Binary Indexed Tree** + +We maintain two binary indexed trees, one records the number of elements smaller than the current position on the left, and the other records the number of elements smaller than the current position on the right. + +We traverse the array, and for the current position, if the number of elements smaller than the current position on the left is greater than or equal to $k$, and the number of elements smaller than the current position on the right is greater than or equal to $k$, then the current position is a `k-big`, and we increment the answer by one. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array. + ### **Python3** diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md index 8a81d9e8d2298..fe85b79ac0f4f 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README.md @@ -48,7 +48,9 @@ **方法一:枚举** -我们直接枚举整数 `num` 的每一位上的数 `val`,若 `val` 能够整除 `num`,那么答案加一。 +我们直接枚举整数 $num$ 的每一位上的数 $val$,若 $val$ 能够整除 $num$,那么答案加一。 + +枚举结束后,返回答案即可。 时间复杂度 $O(\log num)$,空间复杂度 $O(1)$。 @@ -121,12 +123,10 @@ func countDigits(num int) (ans int) { ```ts function countDigits(num: number): number { let ans = 0; - let cur = num; - while (cur !== 0) { - if (num % (cur % 10) === 0) { - ans++; + for (let x = num; x; x = (x / 10) | 0) { + if (num % (x % 10) === 0) { + ++ans; } - cur = Math.floor(cur / 10); } return ans; } diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md index 52580fd3d3717..6cddb8a99f601 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md @@ -43,6 +43,14 @@ ## Solutions +**Solution 1: Enumeration** + +We directly enumerate each digit $val$ of the integer $num$, and if $val$ can divide $num$, we add one to the answer. + +After the enumeration, we return the answer. + +The time complexity is $O(\log num)$, and the space complexity is $O(1)$. + ### **Python3** @@ -108,12 +116,10 @@ func countDigits(num int) (ans int) { ```ts function countDigits(num: number): number { let ans = 0; - let cur = num; - while (cur !== 0) { - if (num % (cur % 10) === 0) { - ans++; + for (let x = num; x; x = (x / 10) | 0) { + if (num % (x % 10) === 0) { + ++ans; } - cur = Math.floor(cur / 10); } return ans; } diff --git a/solution/2500-2599/2520.Count the Digits That Divide a Number/Solution.ts b/solution/2500-2599/2520.Count the Digits That Divide a Number/Solution.ts index 688df2ff5e841..0d51edd275510 100644 --- a/solution/2500-2599/2520.Count the Digits That Divide a Number/Solution.ts +++ b/solution/2500-2599/2520.Count the Digits That Divide a Number/Solution.ts @@ -1,11 +1,9 @@ function countDigits(num: number): number { let ans = 0; - let cur = num; - while (cur !== 0) { - if (num % (cur % 10) === 0) { - ans++; + for (let x = num; x; x = (x / 10) | 0) { + if (num % (x % 10) === 0) { + ++ans; } - cur = Math.floor(cur / 10); } return ans; } diff --git a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md index e3b09bf337f55..f3a85aa95c1d7 100644 --- a/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md +++ b/solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md @@ -68,7 +68,27 @@ 我们定义以下几个状态,其中: -- $f[0]$ 表示从 $source$ +- $f[0]$ 表示从 $source$ 到 $source$ 本身的方法数; +- $f[1]$ 表示从 $source$ 移动到同一列其它行的方法数; +- $f[2]$ 表示从 $source$ 移动到同一行其它列的方法数; +- $f[3]$ 表示从 $source$ 移动到其它行其它列的方法数。 + +初始时,$f[0] = 1$,其余状态均为 $0$。 + +对于每个状态,我们可以根据上一次的状态计算出当前的状态,具体如下: + +$$ +\begin{aligned} +g[0] &= (n - 1) \times f[1] + (m - 1) \times f[2] \\ +g[1] &= f[0] + (n - 2) \times f[1] + (m - 1) \times f[3] \\ +g[2] &= f[0] + (m - 2) \times f[2] + (n - 1) \times f[3] \\ +g[3] &= f[1] + f[2] + (n - 2) \times f[3] + (m - 2) \times f[3] +\end{aligned} +$$ + +我们循环 $k$ 次,最后判断 $source$ 和 $dest$ 是否在同一行或同一列,返回对应的状态即可。 + +时间复杂度 $O(k)$,其中 $k$ 为移动次数。空间复杂度 $O(1)$。