Skip to content

Commit 4577d80

Browse files
authored
feat: add solutions to lc problems: No.2716~2719 (doocs#2225)
1 parent 77f56e2 commit 4577d80

File tree

10 files changed

+99
-70
lines changed

10 files changed

+99
-70
lines changed

solution/2700-2799/2716.Minimize String Length/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
## Solutions
5252

53+
**Solution 1: Hash Table**
54+
55+
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.
56+
57+
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$.
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ It can be proved that there is no sequence of less than three operations that ma
5959

6060
## Solutions
6161

62+
**Solution 1: Find the Positions of 1 and n**
63+
64+
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.
65+
66+
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$.
67+
68+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
69+
6270
<!-- tabs:start -->
6371

6472
### **Python3**

solution/2700-2799/2718.Sum of Matrix After Queries/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Hash Table**
50+
51+
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.
52+
53+
For each query $(t, i, v)$:
54+
55+
- 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$.
56+
- 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$.
57+
58+
Finally, return the answer.
59+
60+
The time complexity is $O(m)$, and the space complexity is $O(n)$. Here, $m$ represents the number of queries.
61+
4962
<!-- tabs:start -->
5063

5164
### **Python3**

solution/2700-2799/2719.Count of Integers/README.md

+20-24
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
**方法一:数位 DP**
5252

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

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

@@ -74,19 +74,19 @@ class Solution:
7474
@cache
7575
def dfs(pos: int, s: int, limit: bool) -> int:
7676
if pos >= len(num):
77-
return 1 if min_sum <= s <= max_sum else 0
77+
return int(min_sum <= s <= max_sum)
7878
up = int(num[pos]) if limit else 9
7979
return (
8080
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
8181
)
8282

8383
mod = 10**9 + 7
8484
num = num2
85-
ans = dfs(0, 0, True)
85+
a = dfs(0, 0, True)
8686
dfs.cache_clear()
8787
num = str(int(num1) - 1)
88-
ans -= dfs(0, 0, True)
89-
return ans % mod
88+
b = dfs(0, 0, True)
89+
return (a - b) % mod
9090
```
9191

9292
### **Java**
@@ -108,11 +108,11 @@ class Solution {
108108
max = max_sum;
109109
num = num2;
110110
f = new Integer[23][220];
111-
int ans = dfs(0, 0, true);
111+
int a = dfs(0, 0, true);
112112
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
113113
f = new Integer[23][220];
114-
ans = (ans - dfs(0, 0, true) + mod) % mod;
115-
return ans;
114+
int b = dfs(0, 0, true);
115+
return (a - b + mod) % mod;
116116
}
117117

118118
private int dfs(int pos, int s, boolean limit) {
@@ -165,8 +165,8 @@ public:
165165
return ans;
166166
};
167167

168-
int ans = dfs(0, 0, true);
169-
for (int i = num1.size() - 1; i >= 0; --i) {
168+
int a = dfs(0, 0, true);
169+
for (int i = num1.size() - 1; ~i; --i) {
170170
if (num1[i] == '0') {
171171
num1[i] = '9';
172172
} else {
@@ -176,8 +176,8 @@ public:
176176
}
177177
num = num1;
178178
memset(f, -1, sizeof(f));
179-
ans -= dfs(0, 0, true);
180-
return (ans + mod) % mod;
179+
int b = dfs(0, 0, true);
180+
return (a - b + mod) % mod;
181181
}
182182
};
183183
```
@@ -218,7 +218,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
218218
}
219219
return ans
220220
}
221-
ans := dfs(0, 0, true)
221+
a := dfs(0, 0, true)
222222
t := []byte(num1)
223223
for i := len(t) - 1; i >= 0; i-- {
224224
if t[i] != '0' {
@@ -234,8 +234,8 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
234234
f[i][j] = -1
235235
}
236236
}
237-
ans -= dfs(0, 0, true)
238-
return (ans%mod + mod) % mod
237+
b := dfs(0, 0, true)
238+
return (a - b + mod) % mod
239239
}
240240
```
241241

@@ -244,9 +244,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
244244
```ts
245245
function count(num1: string, num2: string, min_sum: number, max_sum: number): number {
246246
const mod = 1e9 + 7;
247-
let f: number[][] = Array(23)
248-
.fill(0)
249-
.map(() => Array(220).fill(-1));
247+
const f: number[][] = Array.from({ length: 23 }, () => Array(220).fill(-1));
250248
let num = num2;
251249
const dfs = (pos: number, s: number, limit: boolean): number => {
252250
if (pos >= num.length) {
@@ -265,13 +263,11 @@ function count(num1: string, num2: string, min_sum: number, max_sum: number): nu
265263
}
266264
return ans;
267265
};
268-
let ans = dfs(0, 0, true);
266+
const a = dfs(0, 0, true);
269267
num = (BigInt(num1) - 1n).toString();
270-
f = Array(23)
271-
.fill(0)
272-
.map(() => Array(220).fill(-1));
273-
ans = (ans - dfs(0, 0, true) + mod) % mod;
274-
return ans;
268+
f.forEach(v => v.fill(-1));
269+
const b = dfs(0, 0, true);
270+
return (a - b + mod) % mod;
275271
}
276272
```
277273

solution/2700-2799/2719.Count of Integers/README_EN.md

+33-23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@
4242

4343
## Solutions
4444

45+
**Solution 1: Digit DP**
46+
47+
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.
48+
49+
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.
50+
51+
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$.
52+
53+
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$.
54+
55+
Similar problems:
56+
57+
- [2801. Count Stepping Numbers in Range](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README_EN.md)
58+
4559
<!-- tabs:start -->
4660

4761
### **Python3**
@@ -52,19 +66,19 @@ class Solution:
5266
@cache
5367
def dfs(pos: int, s: int, limit: bool) -> int:
5468
if pos >= len(num):
55-
return 1 if min_sum <= s <= max_sum else 0
69+
return int(min_sum <= s <= max_sum)
5670
up = int(num[pos]) if limit else 9
5771
return (
5872
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
5973
)
6074

6175
mod = 10**9 + 7
6276
num = num2
63-
ans = dfs(0, 0, True)
77+
a = dfs(0, 0, True)
6478
dfs.cache_clear()
6579
num = str(int(num1) - 1)
66-
ans -= dfs(0, 0, True)
67-
return ans % mod
80+
b = dfs(0, 0, True)
81+
return (a - b) % mod
6882
```
6983

7084
### **Java**
@@ -84,11 +98,11 @@ class Solution {
8498
max = max_sum;
8599
num = num2;
86100
f = new Integer[23][220];
87-
int ans = dfs(0, 0, true);
101+
int a = dfs(0, 0, true);
88102
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
89103
f = new Integer[23][220];
90-
ans = (ans - dfs(0, 0, true) + mod) % mod;
91-
return ans;
104+
int b = dfs(0, 0, true);
105+
return (a - b + mod) % mod;
92106
}
93107

94108
private int dfs(int pos, int s, boolean limit) {
@@ -141,8 +155,8 @@ public:
141155
return ans;
142156
};
143157

144-
int ans = dfs(0, 0, true);
145-
for (int i = num1.size() - 1; i >= 0; --i) {
158+
int a = dfs(0, 0, true);
159+
for (int i = num1.size() - 1; ~i; --i) {
146160
if (num1[i] == '0') {
147161
num1[i] = '9';
148162
} else {
@@ -152,8 +166,8 @@ public:
152166
}
153167
num = num1;
154168
memset(f, -1, sizeof(f));
155-
ans -= dfs(0, 0, true);
156-
return (ans + mod) % mod;
169+
int b = dfs(0, 0, true);
170+
return (a - b + mod) % mod;
157171
}
158172
};
159173
```
@@ -194,7 +208,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
194208
}
195209
return ans
196210
}
197-
ans := dfs(0, 0, true)
211+
a := dfs(0, 0, true)
198212
t := []byte(num1)
199213
for i := len(t) - 1; i >= 0; i-- {
200214
if t[i] != '0' {
@@ -210,8 +224,8 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
210224
f[i][j] = -1
211225
}
212226
}
213-
ans -= dfs(0, 0, true)
214-
return (ans%mod + mod) % mod
227+
b := dfs(0, 0, true)
228+
return (a - b + mod) % mod
215229
}
216230
```
217231

@@ -220,9 +234,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
220234
```ts
221235
function count(num1: string, num2: string, min_sum: number, max_sum: number): number {
222236
const mod = 1e9 + 7;
223-
let f: number[][] = Array(23)
224-
.fill(0)
225-
.map(() => Array(220).fill(-1));
237+
const f: number[][] = Array.from({ length: 23 }, () => Array(220).fill(-1));
226238
let num = num2;
227239
const dfs = (pos: number, s: number, limit: boolean): number => {
228240
if (pos >= num.length) {
@@ -241,13 +253,11 @@ function count(num1: string, num2: string, min_sum: number, max_sum: number): nu
241253
}
242254
return ans;
243255
};
244-
let ans = dfs(0, 0, true);
256+
const a = dfs(0, 0, true);
245257
num = (BigInt(num1) - 1n).toString();
246-
f = Array(23)
247-
.fill(0)
248-
.map(() => Array(220).fill(-1));
249-
ans = (ans - dfs(0, 0, true) + mod) % mod;
250-
return ans;
258+
f.forEach(v => v.fill(-1));
259+
const b = dfs(0, 0, true);
260+
return (a - b + mod) % mod;
251261
}
252262
```
253263

solution/2700-2799/2719.Count of Integers/Solution.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Solution {
2525
return ans;
2626
};
2727

28-
int ans = dfs(0, 0, true);
29-
for (int i = num1.size() - 1; i >= 0; --i) {
28+
int a = dfs(0, 0, true);
29+
for (int i = num1.size() - 1; ~i; --i) {
3030
if (num1[i] == '0') {
3131
num1[i] = '9';
3232
} else {
@@ -36,7 +36,7 @@ class Solution {
3636
}
3737
num = num1;
3838
memset(f, -1, sizeof(f));
39-
ans -= dfs(0, 0, true);
40-
return (ans + mod) % mod;
39+
int b = dfs(0, 0, true);
40+
return (a - b + mod) % mod;
4141
}
4242
};

solution/2700-2799/2719.Count of Integers/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
3131
}
3232
return ans
3333
}
34-
ans := dfs(0, 0, true)
34+
a := dfs(0, 0, true)
3535
t := []byte(num1)
3636
for i := len(t) - 1; i >= 0; i-- {
3737
if t[i] != '0' {
@@ -47,6 +47,6 @@ func count(num1 string, num2 string, min_sum int, max_sum int) int {
4747
f[i][j] = -1
4848
}
4949
}
50-
ans -= dfs(0, 0, true)
51-
return (ans%mod + mod) % mod
50+
b := dfs(0, 0, true)
51+
return (a - b + mod) % mod
5252
}

solution/2700-2799/2719.Count of Integers/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public int count(String num1, String num2, int min_sum, int max_sum) {
1212
max = max_sum;
1313
num = num2;
1414
f = new Integer[23][220];
15-
int ans = dfs(0, 0, true);
15+
int a = dfs(0, 0, true);
1616
num = new BigInteger(num1).subtract(BigInteger.ONE).toString();
1717
f = new Integer[23][220];
18-
ans = (ans - dfs(0, 0, true) + mod) % mod;
19-
return ans;
18+
int b = dfs(0, 0, true);
19+
return (a - b + mod) % mod;
2020
}
2121

2222
private int dfs(int pos, int s, boolean limit) {

solution/2700-2799/2719.Count of Integers/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int:
33
@cache
44
def dfs(pos: int, s: int, limit: bool) -> int:
55
if pos >= len(num):
6-
return 1 if min_sum <= s <= max_sum else 0
6+
return int(min_sum <= s <= max_sum)
77
up = int(num[pos]) if limit else 9
88
return (
99
sum(dfs(pos + 1, s + i, limit and i == up) for i in range(up + 1)) % mod
1010
)
1111

1212
mod = 10**9 + 7
1313
num = num2
14-
ans = dfs(0, 0, True)
14+
a = dfs(0, 0, True)
1515
dfs.cache_clear()
1616
num = str(int(num1) - 1)
17-
ans -= dfs(0, 0, True)
18-
return ans % mod
17+
b = dfs(0, 0, True)
18+
return (a - b) % mod

0 commit comments

Comments
 (0)