Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problmes #2516

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions solution/2700-2799/2733.Neither Minimum nor Maximum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@

## 解法

### 方法一
### 方法一:模拟

我们可以先找到数组中的最小值和最大值,分别记为 $mi$ 和 $mx$。然后遍历数组,找到第一个不等于 $mi$ 且不等于 $mx$ 的数字,返回即可。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

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

```java
Expand All @@ -79,10 +84,9 @@ class Solution {
class Solution {
public:
int findNonMinOrMax(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int mx = *max_element(nums.begin(), nums.end());
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
for (int x : nums) {
if (x != mi && x != mx) {
if (x != *mi && x != *mx) {
return x;
}
}
Expand Down
14 changes: 9 additions & 5 deletions solution/2700-2799/2733.Neither Minimum nor Maximum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@

## Solutions

### Solution 1
### Solution 1: Simulation

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.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

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

```java
Expand All @@ -78,10 +83,9 @@ class Solution {
class Solution {
public:
int findNonMinOrMax(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int mx = *max_element(nums.begin(), nums.end());
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
for (int x : nums) {
if (x != mi && x != mx) {
if (x != *mi && x != *mx) {
return x;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Solution {
public:
int findNonMinOrMax(vector<int>& nums) {
int mi = *min_element(nums.begin(), nums.end());
int mx = *max_element(nums.begin(), nums.end());
auto [mi, mx] = minmax_element(nums.begin(), nums.end());
for (int x : nums) {
if (x != mi && x != mx) {
if (x != *mi && x != *mx) {
return x;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Solution:
def findNonMinOrMax(self, nums: List[int]) -> int:
return -1 if len(nums) < 3 else sorted(nums)[1]
mi, mx = min(nums), max(nums)
return next((x for x in nums if x != mi and x != mx), -1)

This file was deleted.

96 changes: 45 additions & 51 deletions solution/2700-2799/2736.Maximum Sum Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,51 @@ class Solution {
}
```

```java
class Solution {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
int n = nums1.length, m = q.length;
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = nums1[i];
a[i][1] = nums2[i];
}
int[][] b = new int[m][3];
for (int i = 0; i < m; i++) {
b[i][0] = q[i][0];
b[i][1] = q[i][1];
b[i][2] = i;
}
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
TreeMap<Integer, Integer> map = new TreeMap<>();
int[] res = new int[m];
int max = -1;
for (int i = m - 1, j = n - 1; i >= 0; i--) {
int x = b[i][0], y = b[i][1], idx = b[i][2];
while (j >= 0 && a[j][0] >= x) {
if (max < a[j][1]) {
max = a[j][1];
Integer key = map.floorKey(a[j][1]);
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
map.remove(key);
key = map.floorKey(key);
}
map.put(max, a[j][0] + a[j][1]);
}
j--;
}
Integer key = map.ceilingKey(y);
if (key == null)
res[idx] = -1;
else
res[idx] = map.get(key);
}
return res;
}
}
```

```cpp
class BinaryIndexedTree {
private:
Expand Down Expand Up @@ -374,55 +419,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][]

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```java
class Solution {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
int n = nums1.length, m = q.length;
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = nums1[i];
a[i][1] = nums2[i];
}
int[][] b = new int[m][3];
for (int i = 0; i < m; i++) {
b[i][0] = q[i][0];
b[i][1] = q[i][1];
b[i][2] = i;
}
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
TreeMap<Integer, Integer> map = new TreeMap<>();
int[] res = new int[m];
int max = -1;
for (int i = m - 1, j = n - 1; i >= 0; i--) {
int x = b[i][0], y = b[i][1], idx = b[i][2];
while (j >= 0 && a[j][0] >= x) {
if (max < a[j][1]) {
max = a[j][1];
Integer key = map.floorKey(a[j][1]);
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
map.remove(key);
key = map.floorKey(key);
}
map.put(max, a[j][0] + a[j][1]);
}
j--;
}
Integer key = map.ceilingKey(y);
if (key == null)
res[idx] = -1;
else
res[idx] = map.get(key);
}
return res;
}
}
```

<!-- tabs:end -->

<!-- end -->
96 changes: 45 additions & 51 deletions solution/2700-2799/2736.Maximum Sum Queries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,51 @@ class Solution {
}
```

```java
class Solution {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
int n = nums1.length, m = q.length;
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = nums1[i];
a[i][1] = nums2[i];
}
int[][] b = new int[m][3];
for (int i = 0; i < m; i++) {
b[i][0] = q[i][0];
b[i][1] = q[i][1];
b[i][2] = i;
}
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
TreeMap<Integer, Integer> map = new TreeMap<>();
int[] res = new int[m];
int max = -1;
for (int i = m - 1, j = n - 1; i >= 0; i--) {
int x = b[i][0], y = b[i][1], idx = b[i][2];
while (j >= 0 && a[j][0] >= x) {
if (max < a[j][1]) {
max = a[j][1];
Integer key = map.floorKey(a[j][1]);
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
map.remove(key);
key = map.floorKey(key);
}
map.put(max, a[j][0] + a[j][1]);
}
j--;
}
Integer key = map.ceilingKey(y);
if (key == null)
res[idx] = -1;
else
res[idx] = map.get(key);
}
return res;
}
}
```

```cpp
class BinaryIndexedTree {
private:
Expand Down Expand Up @@ -376,55 +421,4 @@ function maximumSumQueries(nums1: number[], nums2: number[], queries: number[][]

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```java
class Solution {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] q) {
int n = nums1.length, m = q.length;
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = nums1[i];
a[i][1] = nums2[i];
}
int[][] b = new int[m][3];
for (int i = 0; i < m; i++) {
b[i][0] = q[i][0];
b[i][1] = q[i][1];
b[i][2] = i;
}
Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
Arrays.sort(b, (o1, o2) -> o1[0] - o2[0]);
TreeMap<Integer, Integer> map = new TreeMap<>();
int[] res = new int[m];
int max = -1;
for (int i = m - 1, j = n - 1; i >= 0; i--) {
int x = b[i][0], y = b[i][1], idx = b[i][2];
while (j >= 0 && a[j][0] >= x) {
if (max < a[j][1]) {
max = a[j][1];
Integer key = map.floorKey(a[j][1]);
while (key != null && map.get(key) <= a[j][0] + a[j][1]) {
map.remove(key);
key = map.floorKey(key);
}
map.put(max, a[j][0] + a[j][1]);
}
j--;
}
Integer key = map.ceilingKey(y);
if (key == null)
res[idx] = -1;
else
res[idx] = map.get(key);
}
return res;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ So the answer is 3.

## Solutions

### Solution 1
### Solution 1: Dijkstra's Algorithm

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.

Then, we can use Dijkstra's algorithm to find the shortest distance from the starting point $s$ to all nodes, denoted as $dist$.

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

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of nodes.

<!-- tabs:start -->

Expand Down
6 changes: 5 additions & 1 deletion solution/2700-2799/2739.Total Distance Traveled/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ Total distance traveled is 10km.

## Solutions

### Solution 1
### Solution 1: Simulation

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.

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ It can be proven that 9 is the minimum value out of all partitions.

## Solutions

### Solution 1
### Solution 1: Sorting

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.

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.

<!-- tabs:start -->

Expand Down
20 changes: 19 additions & 1 deletion solution/2700-2799/2741.Special Permutations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,25 @@

## Solutions

### Solution 1
### Solution 1: State Compression Dynamic Programming

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.

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

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:

$$
f[i][j]=
\begin{cases}
1, & i=2^j\\
\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}\\
\end{cases}
$$

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

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ And it can be shown that there are no special substrings with a length of at lea

## Solutions

### Solution 1
### Solution 1: Counting + Two Pointers

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.

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

<!-- tabs:start -->

Expand Down
Loading
Loading