Skip to content

Commit af211c5

Browse files
authored
feat: add solutions to lc problems: No.2800+ (#2184)
1 parent 06533cb commit af211c5

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

solution/2800-2899/2800.Shortest String That Contains Three Strings/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Given three strings <code>a</code>, <code>b</code>, and <code>c</code>, your tas
4343

4444
## Solutions
4545

46+
**Solution 1: Enumeration**
47+
48+
We enumerate all permutations of the three strings, and for each permutation, we merge the three strings to find the shortest string with the smallest lexicographical order.
49+
50+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the maximum length of the three strings.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/2800-2899/2801.Count Stepping Numbers in Range/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@
4141

4242
## Solutions
4343

44+
**Solution 1: Digit DP**
45+
46+
We notice that the problem is asking for the number of stepping numbers in the interval $[low, high]$. For such an interval $[l,..r]$ problem, we can usually consider transforming it into finding the answers for $[1, r]$ and $[1, l-1]$, and then subtracting the latter from the former. Moreover, the problem only involves the relationship between different digits, not the specific values, so we can consider using Digit DP to solve it.
47+
48+
We design a function $dfs(pos, pre, lead, limit)$, which represents the number of schemes when we are currently processing the $pos$-th digit, the previous digit is $pre$, whether the current number only contains leading zeros is $lead$, and whether the current number has reached the upper limit is $limit$. The range of $pos$ is $[0, len(num))$.
49+
50+
The execution logic of the function $dfs(pos, pre, lead, limit)$ is as follows:
51+
52+
If $pos$ exceeds the length of $num$, it means that we have processed all the digits. If $lead$ is true at this time, it means that the current number only contains leading zeros and is not a valid number. We can return $0$ to indicate that the number of schemes is $0$; otherwise, we return $1$ to indicate that the number of schemes is $1$.
53+
54+
Otherwise, we calculate the upper limit $up$ of the current digit, and then enumerate the digit $i$ in the range $[0,..up]$:
55+
56+
- If $i=0$ and $lead$ is true, it means that the current number only contains leading zeros. We recursively calculate the value of $dfs(pos+1,pre, true, limit\ and\ i=up)$ and add it to the answer.
57+
- Otherwise, if $pre$ is $-1$, or the absolute difference between $i$ and $pre$ is $1$, it means that the current number is a valid stepping number. We recursively calculate the value of $dfs(pos+1,i, false, limit\ and\ i=up)$ and add it to the answer.
58+
59+
Finally, we return the answer.
60+
61+
In the main function, we calculate the answers $a$ and $b$ for $[1, high]$ and $[1, low-1]$ respectively. The final answer is $a-b$. Note the modulo operation of the answer.
62+
63+
The time complexity is $O(\log M \times |\Sigma|^2)$, and the space complexity is $O(\log M \times |\Sigma|)$, where $M$ represents the size of the number $high$, and $|\Sigma|$ represents the digit set.
64+
65+
Similar problems:
66+
67+
- [2719. Count of Integers](/solution/2700-2799/2719.Count%20of%20Integers/README_EN.md)
68+
4469
<!-- tabs:start -->
4570

4671
### **Python3**

solution/2800-2899/2802.Find The K-th Lucky Number/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242

4343
## Solutions
4444

45+
**Solution 1: Mathematics**
46+
47+
According to the problem description, a lucky number only contains the digits $4$ and $7$, so the number of $n$-digit lucky numbers is $2^n$.
48+
49+
We initialize $n=1$, then loop to check whether $k$ is greater than $2^n$. If it is, we subtract $2^n$ from $k$ and increment $n$, until $k$ is less than or equal to $2^n$. At this point, we just need to find the $k$-th lucky number among the $n$-digit lucky numbers.
50+
51+
If $k$ is less than or equal to $2^{n-1}$, then the first digit of the $k$-th lucky number is $4$, otherwise the first digit is $7$. Then we subtract $2^{n-1}$ from $k$ and continue to determine the second digit, until all digits of the $n$-digit lucky number are determined.
52+
53+
The time complexity is $O(\log k)$, and the space complexity is $O(\log k)$.
54+
4555
<!-- tabs:start -->
4656

4757
### **Python3**

solution/2800-2899/2814.Minimum Time Takes to Reach Destination Without Drowning/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ So the answer would be -1.
6969

7070
## Solutions
7171

72+
**Solution 1: Two BFS Traversals**
73+
74+
First, we run a BFS (Breadth-First Search) to calculate the shortest distance from each cell to the water, and record it in the array $g$. Then, we run another BFS starting from the cell $(s_i, s_j)$ to find the shortest distance to the target cell $(d_i, d_j)$. During this process, if the adjacent cell $(x, y)$ of the current cell $(i, j)$ satisfies $g[x][y] > t + 1$, then we can move from $(x, y)$ to $(i, j)$.
75+
76+
The time complexity is $O(m \times n)$ and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the array $land$, respectively.
77+
7278
<!-- tabs:start -->
7379

7480
### **Python3**

solution/2800-2899/2819.Minimum Relative Loss After Buying Chocolates/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ It can be shown that these are the minimum possible relative losses.
6666

6767
## Solutions
6868

69+
**Solution 1: Sorting + Binary Search + Prefix Sum**
70+
71+
Based on the problem description, we know:
72+
73+
If $prices[i] \leq k$, then Bob needs to pay $prices[i]$, and Alice doesn't need to pay. Therefore, Bob's relative loss is $prices[i]$. In this case, Bob should choose the chocolate with a lower price to minimize the relative loss.
74+
75+
If $prices[i] > k$, then Bob needs to pay $k$, and Alice needs to pay $prices[i] - k$. Therefore, Bob's relative loss is $k - (prices[i] - k) = 2k - prices[i]$. In this case, Bob should choose the chocolate with a higher price to minimize the relative loss.
76+
77+
Therefore, we first sort the price array $prices$, and then preprocess the prefix sum array $s$, where $s[i]$ represents the sum of the prices of the first $i$ chocolates.
78+
79+
Next, for each query $[k, m]$, we first use binary search to find the index $r$ of the first chocolate with a price greater than $k$. Then, we use binary search again to find the number of chocolates $l$ that should be chosen on the left, so the number of chocolates that should be chosen on the right is $m - l$. At this point, Bob's relative loss is $s[l] + 2k(m - l) - (s[n] - s[n - (m - l)])$.
80+
81+
In the second binary search process mentioned above, we need to judge whether $prices[mid] < 2k - prices[n - (m - mid)]$, where $right$ represents the number of chocolates that should be chosen on the right. If this inequality holds, it means that choosing the chocolate at position $mid$ has a lower relative loss, so we update $l = mid + 1$. Otherwise, it means that the chocolate at position $mid$ has a higher relative loss, so we update $r = mid$.
82+
83+
The time complexity is $O((n + m) \times \log n)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of the arrays $prices$ and $queries$, respectively.
84+
6985
<!-- tabs:start -->
7086

7187
### **Python3**

solution/2800-2899/2820.Election Results/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ Since Ryan and Christine received an equal number of votes, we will display thei
5959

6060
## Solutions
6161

62+
**Solution 1: Window Function + Group Statistics**
63+
64+
We can use the window function `count` to calculate the number of votes each voter gives to the candidates, then use the group statistics function `sum` to calculate the total number of votes for each candidate. Next, we use the window function `rank` to calculate the ranking of each candidate, and finally filter out the candidate who ranks first.
65+
66+
Note that there may be multiple candidates ranking first in the result set, so we need to use `order by` to sort the candidates.
67+
6268
<!-- tabs:start -->
6369

6470
### **SQL**

solution/2800-2899/2827.Number of Beautiful Integers in the Range/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ It can be shown that there is only 1 beautiful integer in the given range.
5959

6060
## Solutions
6161

62+
**Solution 1: Digit DP**
63+
64+
We notice that the problem is asking for the number of beautiful integers in the interval $[low, high]$. For such an interval $[l,..r]$ problem, we can usually consider transforming it into finding the answers for $[1, r]$ and $[1, l-1]$, and then subtracting the latter from the former. Moreover, the problem only involves the relationship between different digits, not the specific values, so we can consider using Digit DP to solve it.
65+
66+
We design a function $dfs(pos, mod, diff, lead, limit)$, which represents the number of schemes when we are currently processing the $pos$-th digit, the result of the current number modulo $k$ is $mod$, the difference between the odd and even digits of the current number is $diff$, whether the current number has leading zeros is $lead$, and whether the current number has reached the upper limit is $limit$.
67+
68+
The execution logic of the function $dfs(pos, mod, diff, lead, limit)$ is as follows:
69+
70+
If $pos$ exceeds the length of $num$, it means that we have processed all the digits. If $mod=0$ and $diff=0$ at this time, it means that the current number meets the requirements of the problem, so we return $1$, otherwise we return $0$.
71+
72+
Otherwise, we calculate the upper limit $up$ of the current digit, and then enumerate the digit $i$ in the range $[0,..up]$:
73+
74+
- If $i=0$ and $lead$ is true, it means that the current number only contains leading zeros. We recursively calculate the value of $dfs(pos + 1, mod, diff, 1, limit\ and\ i=up)$ and add it to the answer.
75+
- Otherwise, we update the value of $diff$ according to the parity of $i$, and then recursively calculate the value of $dfs(pos + 1, (mod \times 10 + i) \bmod k, diff, 0, limit\ and\ i=up)$ and add it to the answer.
76+
77+
Finally, we return the answer.
78+
79+
In the main function, we calculate the answers $a$ and $b$ for $[1, high]$ and $[1, low-1]$ respectively. The final answer is $a-b$.
80+
81+
The time complexity is $O((\log M)^2 \times k \times |\Sigma|)$, and the space complexity is $O((\log M)^2 \times k)$, where $M$ represents the size of the number $high$, and $|\Sigma|$ represents the digit set.
82+
83+
Similar problems:
84+
85+
- [2719. Count of Integers](/solution/2700-2799/2719.Count%20of%20Integers/README_EN.md)
86+
6287
<!-- tabs:start -->
6388

6489
### **Python3**

0 commit comments

Comments
 (0)