Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@

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

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@

**方法一:枚举**

我们直接枚举整数 `num` 的每一位上的数 `val`,若 `val` 能够整除 `num`,那么答案加一。
我们直接枚举整数 $num$ 的每一位上的数 $val$,若 $val$ 能够整除 $num$,那么答案加一。

枚举结束后,返回答案即可。

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

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down