Skip to content

Commit e764667

Browse files
authored
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
1 parent bb1c54e commit e764667

File tree

16 files changed

+203
-180
lines changed

16 files changed

+203
-180
lines changed

solution/2700-2799/2733.Neither Minimum nor Maximum/README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@
4747

4848
## 解法
4949

50-
### 方法一
50+
### 方法一:模拟
51+
52+
我们可以先找到数组中的最小值和最大值,分别记为 $mi$ 和 $mx$。然后遍历数组,找到第一个不等于 $mi$ 且不等于 $mx$ 的数字,返回即可。
53+
54+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
5155

5256
<!-- tabs:start -->
5357

5458
```python
5559
class Solution:
5660
def findNonMinOrMax(self, nums: List[int]) -> int:
57-
return -1 if len(nums) < 3 else sorted(nums)[1]
61+
mi, mx = min(nums), max(nums)
62+
return next((x for x in nums if x != mi and x != mx), -1)
5863
```
5964

6065
```java
@@ -79,10 +84,9 @@ class Solution {
7984
class Solution {
8085
public:
8186
int findNonMinOrMax(vector<int>& nums) {
82-
int mi = *min_element(nums.begin(), nums.end());
83-
int mx = *max_element(nums.begin(), nums.end());
87+
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
8488
for (int x : nums) {
85-
if (x != mi && x != mx) {
89+
if (x != *mi && x != *mx) {
8690
return x;
8791
}
8892
}

solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@
4646

4747
## Solutions
4848

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)$.
5054

5155
<!-- tabs:start -->
5256

5357
```python
5458
class Solution:
5559
def findNonMinOrMax(self, nums: List[int]) -> int:
56-
return -1 if len(nums) < 3 else sorted(nums)[1]
60+
mi, mx = min(nums), max(nums)
61+
return next((x for x in nums if x != mi and x != mx), -1)
5762
```
5863

5964
```java
@@ -78,10 +83,9 @@ class Solution {
7883
class Solution {
7984
public:
8085
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());
8387
for (int x : nums) {
84-
if (x != mi && x != mx) {
88+
if (x != *mi && x != *mx) {
8589
return x;
8690
}
8791
}

solution/2700-2799/2733.Neither Minimum nor Maximum/Solution.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution {
22
public:
33
int findNonMinOrMax(vector<int>& nums) {
4-
int mi = *min_element(nums.begin(), nums.end());
5-
int mx = *max_element(nums.begin(), nums.end());
4+
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
65
for (int x : nums) {
7-
if (x != mi && x != mx) {
6+
if (x != *mi && x != *mx) {
87
return x;
98
}
109
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Solution:
22
def findNonMinOrMax(self, nums: List[int]) -> int:
3-
return -1 if len(nums) < 3 else sorted(nums)[1]
3+
mi, mx = min(nums), max(nums)
4+
return next((x for x in nums if x != mi and x != mx), -1)

solution/2700-2799/2733.Neither Minimum nor Maximum/Solution2.py

-7
This file was deleted.

solution/2700-2799/2736.Maximum Sum Queries/README.md

+45-51
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,51 @@ class Solution {
186186
}
187187
```
188188

189+
```java
190+
class Solution {
191+
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
192+
int n = nums1.length, m = q.length;
193+
int[][] a = new int[n][2];
194+
for (int i = 0; i < n; i++) {
195+
a[i][0] = nums1[i];
196+
a[i][1] = nums2[i];
197+
}
198+
int[][] b = new int[m][3];
199+
for (int i = 0; i < m; i++) {
200+
b[i][0] = q[i][0];
201+
b[i][1] = q[i][1];
202+
b[i][2] = i;
203+
}
204+
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
205+
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
206+
TreeMap<Integer, Integer> map = new TreeMap<>();
207+
int[] res = new int[m];
208+
int max = -1;
209+
for (int i = m - 1, j = n - 1; i >= 0; i--) {
210+
int x = b[i][0], y = b[i][1], idx = b[i][2];
211+
while (j >= 0 && a[j][0] >= x) {
212+
if (max < a[j][1]) {
213+
max = a[j][1];
214+
Integer key = map.floorKey(a[j][1]);
215+
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
216+
map.remove(key);
217+
key = map.floorKey(key);
218+
}
219+
map.put(max, a[j][0] + a[j][1]);
220+
}
221+
j--;
222+
}
223+
Integer key = map.ceilingKey(y);
224+
if (key == null)
225+
res[idx] = -1;
226+
else
227+
res[idx] = map.get(key);
228+
}
229+
return res;
230+
}
231+
}
232+
```
233+
189234
```cpp
190235
class BinaryIndexedTree {
191236
private:
@@ -374,55 +419,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][]
374419

375420
<!-- tabs:end -->
376421

377-
### 方法二
378-
379-
<!-- tabs:start -->
380-
381-
```java
382-
class Solution {
383-
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
384-
int n = nums1.length, m = q.length;
385-
int[][] a = new int[n][2];
386-
for (int i = 0; i < n; i++) {
387-
a[i][0] = nums1[i];
388-
a[i][1] = nums2[i];
389-
}
390-
int[][] b = new int[m][3];
391-
for (int i = 0; i < m; i++) {
392-
b[i][0] = q[i][0];
393-
b[i][1] = q[i][1];
394-
b[i][2] = i;
395-
}
396-
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
397-
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
398-
TreeMap<Integer, Integer> map = new TreeMap<>();
399-
int[] res = new int[m];
400-
int max = -1;
401-
for (int i = m - 1, j = n - 1; i >= 0; i--) {
402-
int x = b[i][0], y = b[i][1], idx = b[i][2];
403-
while (j >= 0 && a[j][0] >= x) {
404-
if (max < a[j][1]) {
405-
max = a[j][1];
406-
Integer key = map.floorKey(a[j][1]);
407-
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
408-
map.remove(key);
409-
key = map.floorKey(key);
410-
}
411-
map.put(max, a[j][0] + a[j][1]);
412-
}
413-
j--;
414-
}
415-
Integer key = map.ceilingKey(y);
416-
if (key == null)
417-
res[idx] = -1;
418-
else
419-
res[idx] = map.get(key);
420-
}
421-
return res;
422-
}
423-
}
424-
```
425-
426-
<!-- tabs:end -->
427-
428422
<!-- end -->

solution/2700-2799/2736.Maximum Sum Queries/README_EN.md

+45-51
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,51 @@ class Solution {
188188
}
189189
```
190190

191+
```java
192+
class Solution {
193+
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
194+
int n = nums1.length, m = q.length;
195+
int[][] a = new int[n][2];
196+
for (int i = 0; i < n; i++) {
197+
a[i][0] = nums1[i];
198+
a[i][1] = nums2[i];
199+
}
200+
int[][] b = new int[m][3];
201+
for (int i = 0; i < m; i++) {
202+
b[i][0] = q[i][0];
203+
b[i][1] = q[i][1];
204+
b[i][2] = i;
205+
}
206+
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
207+
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
208+
TreeMap<Integer, Integer> map = new TreeMap<>();
209+
int[] res = new int[m];
210+
int max = -1;
211+
for (int i = m - 1, j = n - 1; i >= 0; i--) {
212+
int x = b[i][0], y = b[i][1], idx = b[i][2];
213+
while (j >= 0 && a[j][0] >= x) {
214+
if (max < a[j][1]) {
215+
max = a[j][1];
216+
Integer key = map.floorKey(a[j][1]);
217+
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
218+
map.remove(key);
219+
key = map.floorKey(key);
220+
}
221+
map.put(max, a[j][0] + a[j][1]);
222+
}
223+
j--;
224+
}
225+
Integer key = map.ceilingKey(y);
226+
if (key == null)
227+
res[idx] = -1;
228+
else
229+
res[idx] = map.get(key);
230+
}
231+
return res;
232+
}
233+
}
234+
```
235+
191236
```cpp
192237
class BinaryIndexedTree {
193238
private:
@@ -376,55 +421,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][]
376421

377422
<!-- tabs:end -->
378423

379-
### Solution 2
380-
381-
<!-- tabs:start -->
382-
383-
```java
384-
class Solution {
385-
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
386-
int n = nums1.length, m = q.length;
387-
int[][] a = new int[n][2];
388-
for (int i = 0; i < n; i++) {
389-
a[i][0] = nums1[i];
390-
a[i][1] = nums2[i];
391-
}
392-
int[][] b = new int[m][3];
393-
for (int i = 0; i < m; i++) {
394-
b[i][0] = q[i][0];
395-
b[i][1] = q[i][1];
396-
b[i][2] = i;
397-
}
398-
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
399-
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
400-
TreeMap<Integer, Integer> map = new TreeMap<>();
401-
int[] res = new int[m];
402-
int max = -1;
403-
for (int i = m - 1, j = n - 1; i >= 0; i--) {
404-
int x = b[i][0], y = b[i][1], idx = b[i][2];
405-
while (j >= 0 && a[j][0] >= x) {
406-
if (max < a[j][1]) {
407-
max = a[j][1];
408-
Integer key = map.floorKey(a[j][1]);
409-
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
410-
map.remove(key);
411-
key = map.floorKey(key);
412-
}
413-
map.put(max, a[j][0] + a[j][1]);
414-
}
415-
j--;
416-
}
417-
Integer key = map.ceilingKey(y);
418-
if (key == null)
419-
res[idx] = -1;
420-
else
421-
res[idx] = map.get(key);
422-
}
423-
return res;
424-
}
425-
}
426-
```
427-
428-
<!-- tabs:end -->
429-
430424
<!-- end -->

solution/2700-2799/2737.Find the Closest Marked Node/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ So the answer is 3.
6666

6767
## Solutions
6868

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.
7078

7179
<!-- tabs:start -->
7280

solution/2700-2799/2739.Total Distance Traveled/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ Total distance traveled is 10km.
4646

4747
## Solutions
4848

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)$.
5054

5155
<!-- tabs:start -->
5256

solution/2700-2799/2740.Find the Value of the Partition/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ It can be proven that 9 is the minimum value out of all partitions.
5757

5858
## Solutions
5959

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.
6165

6266
<!-- tabs:start -->
6367

solution/2700-2799/2741.Special Permutations/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,25 @@
4141

4242
## Solutions
4343

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.
4563

4664
<!-- tabs:start -->
4765

solution/2700-2799/2743.Count Substrings Without Repeating Character/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ And it can be shown that there are no special substrings with a length of at lea
4949

5050
## Solutions
5151

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$.
5357

5458
<!-- tabs:start -->
5559

0 commit comments

Comments
 (0)