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
feat: update solutions to lc problmes (doocs#2516)
* NO.2733.Neither Minimum nor Maximum
* NO.2736.Maximum Sum Queries
* NO.2737.Find the Closest Marked Node
* NO.2739.Total Distance Traveled
* NO.2740.Find the Value of the Partition
* NO.2741.Special Permutations
* NO.2743.Count Substrings Without Repeating Character
* NO.2881.Create a New Column
* NO.2912.Number of Ways to Reach Destination in the Grid
Copy file name to clipboardexpand all lines: solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md
+9-5
Original file line number
Diff line number
Diff line change
@@ -46,14 +46,19 @@
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Simulation
50
+
51
+
First, we find the minimum and maximum values in the array, denoted as $mi$ and $mx$ respectively. Then, we traverse the array and find the first number that is not equal to $mi$ and not equal to $mx$, and return it.
52
+
53
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
50
54
51
55
<!-- tabs:start -->
52
56
53
57
```python
54
58
classSolution:
55
59
deffindNonMinOrMax(self, nums: List[int]) -> int:
56
-
return-1iflen(nums) <3elsesorted(nums)[1]
60
+
mi, mx =min(nums), max(nums)
61
+
returnnext((x for x in nums if x != mi and x != mx), -1)
57
62
```
58
63
59
64
```java
@@ -78,10 +83,9 @@ class Solution {
78
83
classSolution {
79
84
public:
80
85
int findNonMinOrMax(vector<int>& nums) {
81
-
int mi = *min_element(nums.begin(), nums.end());
82
-
int mx = *max_element(nums.begin(), nums.end());
86
+
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
Copy file name to clipboardexpand all lines: solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,15 @@ So the answer is 3.
66
66
67
67
## Solutions
68
68
69
-
### Solution 1
69
+
### Solution 1: Dijkstra's Algorithm
70
+
71
+
First, we construct an adjacency matrix $g$ based on the edge information provided in the problem, where $g[i][j]$ represents the distance from node $i$ to node $j$. If such an edge does not exist, then $g[i][j]$ is positive infinity.
72
+
73
+
Then, we can use Dijkstra's algorithm to find the shortest distance from the starting point $s$ to all nodes, denoted as $dist$.
74
+
75
+
Finally, we traverse all the marked nodes and find the marked node with the smallest distance. If the distance is positive infinity, we return $-1$.
76
+
77
+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of nodes.
Copy file name to clipboardexpand all lines: solution/2700-2799/2739.Total Distance Traveled/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,11 @@ Total distance traveled is 10km.
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Simulation
50
+
51
+
We can simulate the process of the truck's movement. Each time, it consumes 1 liter of fuel from the main fuel tank and travels 10 kilometers. Whenever the fuel in the main fuel tank is consumed by 5 liters, if there is fuel in the auxiliary fuel tank, 1 liter of fuel is transferred from the auxiliary fuel tank to the main fuel tank. The simulation continues until the fuel in the main fuel tank is exhausted.
52
+
53
+
The time complexity is $O(n + m)$, where $n$ and $m$ are the amounts of fuel in the main and auxiliary fuel tanks, respectively. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/2700-2799/2740.Find the Value of the Partition/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,11 @@ It can be proven that 9 is the minimum value out of all partitions.
57
57
58
58
## Solutions
59
59
60
-
### Solution 1
60
+
### Solution 1: Sorting
61
+
62
+
The problem requires us to minimize the partition value. Therefore, we can sort the array and then take the minimum difference between two adjacent numbers.
63
+
64
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.
Copy file name to clipboardexpand all lines: solution/2700-2799/2741.Special Permutations/README_EN.md
+19-1
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,25 @@
41
41
42
42
## Solutions
43
43
44
-
### Solution 1
44
+
### Solution 1: State Compression Dynamic Programming
45
+
46
+
We notice that the maximum length of the array in the problem does not exceed $14$. Therefore, we can use an integer to represent the current state, where the $i$-th bit is $1$ if the $i$-th number in the array has been selected, and $0$ if it has not been selected.
47
+
48
+
We define $f[i][j]$ as the number of schemes where the current selected integer state is $i$, and the index of the last selected integer is $j$. Initially, $f[0][0]=0$, and the answer is $\sum_{j=0}^{n-1}f[2^n-1][j]$.
49
+
50
+
Considering $f[i][j]$, if only one number is currently selected, then $f[i][j]=1$. Otherwise, we can enumerate the index $k$ of the last selected number. If the numbers corresponding to $k$ and $j$ meet the requirements of the problem, then $f[i][j]$ can be transferred from $f[i \oplus 2^j][k]$. That is:
51
+
52
+
$$
53
+
f[i][j]=
54
+
\begin{cases}
55
+
1, & i=2^j\\
56
+
\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \text{ and nums}[j] \text{ and nums}[k] \text{ meet the requirements of the problem}\\
57
+
\end{cases}
58
+
$$
59
+
60
+
The final answer is $\sum_{j=0}^{n-1}f[2^n-1][j]$. Note that the answer may be very large, so we need to take the modulus of $10^9+7$.
61
+
62
+
The time complexity is $O(n^2 \times 2^n)$, and the space complexity is $O(n \times 2^n)$. Here, $n$ is the length of the array.
Copy file name to clipboardexpand all lines: solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,11 @@ And it can be shown that there are no special substrings with a length of at lea
49
49
50
50
## Solutions
51
51
52
-
### Solution 1
52
+
### Solution 1: Counting + Two Pointers
53
+
54
+
We use two pointers $j$ and $i$ to represent the left and right boundaries of the current substring, and an array $cnt$ of length $26$ to count the occurrence of each character in the current substring. We traverse the string from left to right. Each time we traverse to position $i$, we increase the occurrence of $s[i]$, and then check whether $s[i]$ appears at least twice. If so, we need to decrease the occurrence of $s[j]$ and move $j$ one step to the right, until the occurrence of $s[i]$ does not exceed once. In this way, we get the length of the longest special substring ending with $s[i]$, which is $i - j + 1$, so the number of special substrings ending with $s[i]$ is $i - j + 1$. Finally, we add up the number of special substrings ending at each position to get the answer.
55
+
56
+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string $s$, and $C$ is the size of the character set. In this problem, the character set consists of lowercase English letters, so $C = 26$.
0 commit comments