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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 2 additions & 3 deletions
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
}
Lines changed: 2 additions & 1 deletion
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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 45 additions & 51 deletions
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

Lines changed: 45 additions & 51 deletions
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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

0 commit comments

Comments
 (0)