Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2716~2719 #2225

Merged
merged 1 commit into from
Jan 16, 2024
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
6 changes: 6 additions & 0 deletions solution/2700-2799/2716.Minimize String Length/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@

## Solutions

**Solution 1: Hash Table**

The problem can actually be transformed into finding the number of different characters in the string. Therefore, we only need to count the number of different characters in the string.

The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, the character set is lowercase English letters, so $C=26$.

<!-- tabs:start -->

### **Python3**
Expand Down
8 changes: 8 additions & 0 deletions solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ It can be proved that there is no sequence of less than three operations that ma

## Solutions

**Solution 1: Find the Positions of 1 and n**

We can first find the indices $i$ and $j$ of $1$ and $n$, respectively. Then, based on the relative positions of $i$ and $j$, we can determine the number of swaps required.

If $i < j$, the number of swaps required is $i + n - j - 1$. If $i > j$, the number of swaps required is $i + n - j - 2$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down
13 changes: 13 additions & 0 deletions solution/2700-2799/2718.Sum of Matrix After Queries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@

## Solutions

**Solution 1: Hash Table**

Since the value of each row and column depends on the last modification, we can traverse all queries in reverse order and use hash tables $row$ and $col$ to record which rows and columns have been modified.

For each query $(t, i, v)$:

- If $t = 0$, we check whether the $i$th row has been modified. If not, we add $v \times (n - |col|)$ to the answer, where $|col|$ represents the size of $col$, and then add $i$ to $row$.
- If $t = 1$, we check whether the $i$th column has been modified. If not, we add $v \times (n - |row|)$ to the answer, where $|row|$ represents the size of $row$, and then add $i$ to $col$.

Finally, return the answer.

The time complexity is $O(m)$, and the space complexity is $O(n)$. Here, $m$ represents the number of queries.

<!-- tabs:start -->

### **Python3**
Expand Down
44 changes: 20 additions & 24 deletions solution/2700-2799/2719.Count of Integers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

**方法一:数位 DP**

题目实际上求的是区间 $[num1,..num2]$ 中数位和在 $[min\_sum,..max\_sum]$ 的数的个数。对于这种区间 $[l,..r]$ 的问题,我们可以转化为求 $[1,..r]$ 和 $[1,..l-1]$ 的答案,然后相减即可。
题目实际上求的是区间 $[num1,..num2]$ 中,数位和在 $[min\_sum,..max\_sum]$ 的数的个数。对于这种区间 $[l,..r]$ 的问题,我们可以考虑转化为求 $[1,..r]$ 和 $[1,..l-1]$ 的答案,然后相减即可。

对于 $[1,..r]$ 的答案,我们可以使用数位 DP 来求解。我们设计一个函数 $dfs(pos, s, limit)$ 表示当前处理到第 $pos$ 位,数位和为 $s$,当前数是否有上界限制 $limit$ 的方案数。其中 $pos$ 从高到低枚举。

Expand All @@ -74,19 +74,19 @@ class Solution:
@cache
def dfs(pos: int, s: int, limit: bool) -> int:
if pos >= len(num):
return 1 if min_sum <= s <= max_sum else 0
return int(min_sum <= s <= max_sum)
up = int(num[pos]) if limit else 9
return (
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
)

mod = 10**9 + 7
num = num2
ans = dfs(0, 0, True)
a = dfs(0, 0, True)
dfs.cache_clear()
num = str(int(num1) - 1)
ans -= dfs(0, 0, True)
return ans % mod
b = dfs(0, 0, True)
return (a - b) % mod
```

### **Java**
Expand All @@ -108,11 +108,11 @@ class Solution {
max = max_sum;
num = num2;
f = new Integer[23][220];
int ans = dfs(0, 0, true);
int a = dfs(0, 0, true);
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
f = new Integer[23][220];
ans = (ans - dfs(0, 0, true) + mod) % mod;
return ans;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}

private int dfs(int pos, int s, boolean limit) {
Expand Down Expand Up @@ -165,8 +165,8 @@ public:
return ans;
};

int ans = dfs(0, 0, true);
for (int i = num1.size() - 1; i >= 0; --i) {
int a = dfs(0, 0, true);
for (int i = num1.size() - 1; ~i; --i) {
if (num1[i] == '0') {
num1[i] = '9';
} else {
Expand All @@ -176,8 +176,8 @@ public:
}
num = num1;
memset(f, -1, sizeof(f));
ans -= dfs(0, 0, true);
return (ans + mod) % mod;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}
};
```
Expand Down Expand Up @@ -218,7 +218,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
}
return ans
}
ans := dfs(0, 0, true)
a := dfs(0, 0, true)
t := []byte(num1)
for i := len(t) - 1; i >= 0; i-- {
if t[i] != '0' {
Expand All @@ -234,8 +234,8 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
f[i][j] = -1
}
}
ans -= dfs(0, 0, true)
return (ans%mod + mod) % mod
b := dfs(0, 0, true)
return (a - b + mod) % mod
}
```

Expand All @@ -244,9 +244,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
```ts
function count(num1: string, num2: string, min_sum: number, max_sum: number): number {
const mod = 1e9 + 7;
let f: number[][] = Array(23)
.fill(0)
.map(() => Array(220).fill(-1));
const f: number[][] = Array.from({ length: 23 }, () => Array(220).fill(-1));
let num = num2;
const dfs = (pos: number, s: number, limit: boolean): number => {
if (pos >= num.length) {
Expand All @@ -265,13 +263,11 @@ function count(num1: string, num2: string, min_sum: number, max_sum: number): nu
}
return ans;
};
let ans = dfs(0, 0, true);
const a = dfs(0, 0, true);
num = (BigInt(num1) - 1n).toString();
f = Array(23)
.fill(0)
.map(() => Array(220).fill(-1));
ans = (ans - dfs(0, 0, true) + mod) % mod;
return ans;
f.forEach(v => v.fill(-1));
const b = dfs(0, 0, true);
return (a - b + mod) % mod;
}
```

Expand Down
56 changes: 33 additions & 23 deletions solution/2700-2799/2719.Count of Integers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@

## Solutions

**Solution 1: Digit DP**

The problem is actually asking for the number of integers in the range $[num1,..num2]$ whose digit sum is in the range $[min\_sum,..max\_sum]$. For this kind of range $[l,..r]$ problem, we can consider transforming it into finding the answers for $[1,..r]$ and $[1,..l-1]$, and then subtracting the latter from the former.

For the answer to $[1,..r]$, we can use digit DP to solve it. We design a function $dfs(pos, s, limit)$, which represents the number of schemes when we are currently processing the $pos$th digit, the digit sum is $s$, and whether the current number has an upper limit $limit$. Here, $pos$ is enumerated from high to low.

For $dfs(pos, s, limit)$, we can enumerate the value of the current digit $i$, and then recursively calculate $dfs(pos+1, s+i, limit \bigcap i==up)$, where $up$ represents the upper limit of the current digit. If $limit$ is true, then $up$ is the upper limit of the current digit, otherwise $up$ is $9$. If $pos$ is greater than or equal to the length of $num$, then we can judge whether $s$ is in the range $[min\_sum,..max\_sum]$. If it is, return $1$, otherwise return $0$.

The time complexity is $O(10 \times n \times max\_sum)$, and the space complexity is $O(n \times max\_sum)$. Here, $n$ represents the length of $num$.

Similar problems:

- [2801. Count Stepping Numbers in Range](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README_EN.md)

<!-- tabs:start -->

### **Python3**
Expand All @@ -52,19 +66,19 @@ class Solution:
@cache
def dfs(pos: int, s: int, limit: bool) -> int:
if pos >= len(num):
return 1 if min_sum <= s <= max_sum else 0
return int(min_sum <= s <= max_sum)
up = int(num[pos]) if limit else 9
return (
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
)

mod = 10**9 + 7
num = num2
ans = dfs(0, 0, True)
a = dfs(0, 0, True)
dfs.cache_clear()
num = str(int(num1) - 1)
ans -= dfs(0, 0, True)
return ans % mod
b = dfs(0, 0, True)
return (a - b) % mod
```

### **Java**
Expand All @@ -84,11 +98,11 @@ class Solution {
max = max_sum;
num = num2;
f = new Integer[23][220];
int ans = dfs(0, 0, true);
int a = dfs(0, 0, true);
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
f = new Integer[23][220];
ans = (ans - dfs(0, 0, true) + mod) % mod;
return ans;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}

private int dfs(int pos, int s, boolean limit) {
Expand Down Expand Up @@ -141,8 +155,8 @@ public:
return ans;
};

int ans = dfs(0, 0, true);
for (int i = num1.size() - 1; i >= 0; --i) {
int a = dfs(0, 0, true);
for (int i = num1.size() - 1; ~i; --i) {
if (num1[i] == '0') {
num1[i] = '9';
} else {
Expand All @@ -152,8 +166,8 @@ public:
}
num = num1;
memset(f, -1, sizeof(f));
ans -= dfs(0, 0, true);
return (ans + mod) % mod;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}
};
```
Expand Down Expand Up @@ -194,7 +208,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
}
return ans
}
ans := dfs(0, 0, true)
a := dfs(0, 0, true)
t := []byte(num1)
for i := len(t) - 1; i >= 0; i-- {
if t[i] != '0' {
Expand All @@ -210,8 +224,8 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
f[i][j] = -1
}
}
ans -= dfs(0, 0, true)
return (ans%mod + mod) % mod
b := dfs(0, 0, true)
return (a - b + mod) % mod
}
```

Expand All @@ -220,9 +234,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
```ts
function count(num1: string, num2: string, min_sum: number, max_sum: number): number {
const mod = 1e9 + 7;
let f: number[][] = Array(23)
.fill(0)
.map(() => Array(220).fill(-1));
const f: number[][] = Array.from({ length: 23 }, () => Array(220).fill(-1));
let num = num2;
const dfs = (pos: number, s: number, limit: boolean): number => {
if (pos >= num.length) {
Expand All @@ -241,13 +253,11 @@ function count(num1: string, num2: string, min_sum: number, max_sum: number): nu
}
return ans;
};
let ans = dfs(0, 0, true);
const a = dfs(0, 0, true);
num = (BigInt(num1) - 1n).toString();
f = Array(23)
.fill(0)
.map(() => Array(220).fill(-1));
ans = (ans - dfs(0, 0, true) + mod) % mod;
return ans;
f.forEach(v => v.fill(-1));
const b = dfs(0, 0, true);
return (a - b + mod) % mod;
}
```

Expand Down
8 changes: 4 additions & 4 deletions solution/2700-2799/2719.Count of Integers/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Solution {
return ans;
};

int ans = dfs(0, 0, true);
for (int i = num1.size() - 1; i >= 0; --i) {
int a = dfs(0, 0, true);
for (int i = num1.size() - 1; ~i; --i) {
if (num1[i] == '0') {
num1[i] = '9';
} else {
Expand All @@ -36,7 +36,7 @@ class Solution {
}
num = num1;
memset(f, -1, sizeof(f));
ans -= dfs(0, 0, true);
return (ans + mod) % mod;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}
};
6 changes: 3 additions & 3 deletions solution/2700-2799/2719.Count of Integers/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
}
return ans
}
ans := dfs(0, 0, true)
a := dfs(0, 0, true)
t := []byte(num1)
for i := len(t) - 1; i >= 0; i-- {
if t[i] != '0' {
Expand All @@ -47,6 +47,6 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
f[i][j] = -1
}
}
ans -= dfs(0, 0, true)
return (ans%mod + mod) % mod
b := dfs(0, 0, true)
return (a - b + mod) % mod
}
6 changes: 3 additions & 3 deletions solution/2700-2799/2719.Count of Integers/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public int count(String num1, String num2, int min_sum, int max_sum) {
max = max_sum;
num = num2;
f = new Integer[23][220];
int ans = dfs(0, 0, true);
int a = dfs(0, 0, true);
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
f = new Integer[23][220];
ans = (ans - dfs(0, 0, true) + mod) % mod;
return ans;
int b = dfs(0, 0, true);
return (a - b + mod) % mod;
}

private int dfs(int pos, int s, boolean limit) {
Expand Down
8 changes: 4 additions & 4 deletions solution/2700-2799/2719.Count of Integers/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int:
@cache
def dfs(pos: int, s: int, limit: bool) -> int:
if pos >= len(num):
return 1 if min_sum <= s <= max_sum else 0
return int(min_sum <= s <= max_sum)
up = int(num[pos]) if limit else 9
return (
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
)

mod = 10**9 + 7
num = num2
ans = dfs(0, 0, True)
a = dfs(0, 0, True)
dfs.cache_clear()
num = str(int(num1) - 1)
ans -= dfs(0, 0, True)
return ans % mod
b = dfs(0, 0, True)
return (a - b) % mod
Loading