Skip to content

Commit 8f7ca43

Browse files
authored
chore: update solutions to lc problems: No.2519,2520,2912 (doocs#1885)
* No.2519.Count the Number of K-Big Indices * No.2520.Count the Digits That Divide a Number * No.2912.Number of Ways to Reach Destination in the Grid
1 parent 939d0df commit 8f7ca43

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

Diff for: solution/2500-2599/2519.Count the Number of K-Big Indices/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151

5252
**方法一:树状数组**
5353

54-
维护两个树状数组,一个记录当前位置左边小于当前位置的数的个数,另一个记录当前位置右边小于当前位置的数的个数。
54+
我们维护两个树状数组,一个记录当前位置左边小于当前位置的数的个数,另一个记录当前位置右边小于当前位置的数的个数。
5555

5656
遍历数组,对于当前位置,如果左边小于当前位置的数的个数大于等于 $k$,且右边小于当前位置的数的个数大于等于 $k$,则当前位置是 $k-big$,答案加一。
5757

58-
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
58+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
5959

6060
<!-- tabs:start -->
6161

Diff for: solution/2500-2599/2519.Count the Number of K-Big Indices/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444

4545
## Solutions
4646

47+
**Solution 1: Binary Indexed Tree**
48+
49+
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.
50+
51+
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.
52+
53+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**

Diff for: solution/2500-2599/2520.Count the Digits That Divide a Number/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848

4949
**方法一:枚举**
5050

51-
我们直接枚举整数 `num` 的每一位上的数 `val`,若 `val` 能够整除 `num`,那么答案加一。
51+
我们直接枚举整数 $num$ 的每一位上的数 $val$,若 $val$ 能够整除 $num$,那么答案加一。
52+
53+
枚举结束后,返回答案即可。
5254

5355
时间复杂度 $O(\log num)$,空间复杂度 $O(1)$。
5456

@@ -121,12 +123,10 @@ func countDigits(num int) (ans int) {
121123
```ts
122124
function countDigits(num: number): number {
123125
let ans = 0;
124-
let cur = num;
125-
while (cur !== 0) {
126-
if (num % (cur % 10) === 0) {
127-
ans++;
126+
for (let x = num; x; x = (x / 10) | 0) {
127+
if (num % (x % 10) === 0) {
128+
++ans;
128129
}
129-
cur = Math.floor(cur / 10);
130130
}
131131
return ans;
132132
}

Diff for: solution/2500-2599/2520.Count the Digits That Divide a Number/README_EN.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Enumeration**
47+
48+
We directly enumerate each digit $val$ of the integer $num$, and if $val$ can divide $num$, we add one to the answer.
49+
50+
After the enumeration, we return the answer.
51+
52+
The time complexity is $O(\log num)$, and the space complexity is $O(1)$.
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
@@ -108,12 +116,10 @@ func countDigits(num int) (ans int) {
108116
```ts
109117
function countDigits(num: number): number {
110118
let ans = 0;
111-
let cur = num;
112-
while (cur !== 0) {
113-
if (num % (cur % 10) === 0) {
114-
ans++;
119+
for (let x = num; x; x = (x / 10) | 0) {
120+
if (num % (x % 10) === 0) {
121+
++ans;
115122
}
116-
cur = Math.floor(cur / 10);
117123
}
118124
return ans;
119125
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
function countDigits(num: number): number {
22
let ans = 0;
3-
let cur = num;
4-
while (cur !== 0) {
5-
if (num % (cur % 10) === 0) {
6-
ans++;
3+
for (let x = num; x; x = (x / 10) | 0) {
4+
if (num % (x % 10) === 0) {
5+
++ans;
76
}
8-
cur = Math.floor(cur / 10);
97
}
108
return ans;
119
}

Diff for: solution/2900-2999/2912.Number of Ways to Reach Destination in the Grid/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,27 @@
6868

6969
我们定义以下几个状态,其中:
7070

71-
- $f[0]$ 表示从 $source$
71+
- $f[0]$ 表示从 $source$ 到 $source$ 本身的方法数;
72+
- $f[1]$ 表示从 $source$ 移动到同一列其它行的方法数;
73+
- $f[2]$ 表示从 $source$ 移动到同一行其它列的方法数;
74+
- $f[3]$ 表示从 $source$ 移动到其它行其它列的方法数。
75+
76+
初始时,$f[0] = 1$,其余状态均为 $0$。
77+
78+
对于每个状态,我们可以根据上一次的状态计算出当前的状态,具体如下:
79+
80+
$$
81+
\begin{aligned}
82+
g[0] &= (n - 1) \times f[1] + (m - 1) \times f[2] \\
83+
g[1] &= f[0] + (n - 2) \times f[1] + (m - 1) \times f[3] \\
84+
g[2] &= f[0] + (m - 2) \times f[2] + (n - 1) \times f[3] \\
85+
g[3] &= f[1] + f[2] + (n - 2) \times f[3] + (m - 2) \times f[3]
86+
\end{aligned}
87+
$$
88+
89+
我们循环 $k$ 次,最后判断 $source$ 和 $dest$ 是否在同一行或同一列,返回对应的状态即可。
90+
91+
时间复杂度 $O(k)$,其中 $k$ 为移动次数。空间复杂度 $O(1)$。
7292

7393
<!-- tabs:start -->
7494

0 commit comments

Comments
 (0)