You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2700-2799/2716.Minimize String Length/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,12 @@
50
50
51
51
## Solutions
52
52
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$.
Copy file name to clipboardexpand all lines: solution/2700-2799/2717.Semi-Ordered Permutation/README_EN.md
+8
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,14 @@ It can be proved that there is no sequence of less than three operations that ma
59
59
60
60
## Solutions
61
61
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)$.
Copy file name to clipboardexpand all lines: solution/2700-2799/2718.Sum of Matrix After Queries/README_EN.md
+13
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,19 @@
46
46
47
47
## Solutions
48
48
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.
Copy file name to clipboardexpand all lines: solution/2700-2799/2719.Count of Integers/README_EN.md
+33-23
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,20 @@
42
42
43
43
## Solutions
44
44
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
+
45
59
<!-- tabs:start -->
46
60
47
61
### **Python3**
@@ -52,19 +66,19 @@ class Solution:
52
66
@cache
53
67
defdfs(pos: int, s: int, limit: bool) -> int:
54
68
if pos >=len(num):
55
-
return1ifmin_sum <= s <= max_sumelse0
69
+
returnint(min_sum <= s <= max_sum)
56
70
up =int(num[pos]) if limit else9
57
71
return (
58
72
sum(dfs(pos +1, s + i, limit and i == up) for i inrange(up +1)) % mod
59
73
)
60
74
61
75
mod =10**9+7
62
76
num = num2
63
-
ans= dfs(0, 0, True)
77
+
a= dfs(0, 0, True)
64
78
dfs.cache_clear()
65
79
num =str(int(num1) -1)
66
-
ans -= dfs(0, 0, True)
67
-
returnans% mod
80
+
b = dfs(0, 0, True)
81
+
return(a - b)% mod
68
82
```
69
83
70
84
### **Java**
@@ -84,11 +98,11 @@ class Solution {
84
98
max = max_sum;
85
99
num = num2;
86
100
f =newInteger[23][220];
87
-
intans= dfs(0, 0, true);
101
+
inta= dfs(0, 0, true);
88
102
num =newBigInteger(num1).subtract(BigInteger.ONE).toString();
0 commit comments