Skip to content

Commit da21eee

Browse files
authored
feat: update solutions to lc problems: No.2293~2295 (#3285)
1 parent efd1ac6 commit da21eee

File tree

11 files changed

+85
-74
lines changed

11 files changed

+85
-74
lines changed

solution/2200-2299/2293.Min Max Game/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ tags:
7777

7878
根据题意,我们可以模拟整个过程,最后剩下的数字即为答案。在实现上,我们不需要额外创建数组,直接在原数组上进行操作即可。
7979

80-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
80+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$
8181

8282
<!-- tabs:start -->
8383

solution/2200-2299/2293.Min Max Game/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ Third: nums = [1]
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Simulation
73+
74+
According to the problem statement, we can simulate the entire process, and the remaining number will be the answer. In implementation, we do not need to create an additional array; we can directly operate on the original array.
75+
76+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
7377

7478
<!-- tabs:start -->
7579

solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ tags:
8383

8484
### 方法一:贪心 + 排序
8585

86-
题目是要求划分子序列,而不是子数组,因此子序列中的元素可以不连续。我们可以将数组 `nums` 排序,假设当前子序列的第一个元素为 $a$,则子序列中的最大值和最小值的差值不会超过 $k$。因此我们可以遍历数组 `nums`,如果当前元素 $b$ 与 $a$ 的差值大于 $k$,则更新 $a$ 为 $b$,并将子序列数目加 1。遍历结束后,即可得到最少子序列数目,注意初始时子序列数目为 $1$。
86+
题目要求划分子序列,而不是子数组,因此子序列中的元素可以不连续。我们可以将数组 $\textit{nums}$ 排序,假设当前子序列的第一个元素为 $a$,则子序列中的最大值和最小值的差值不会超过 $k$。因此我们可以遍历数组 $\textit{nums}$,如果当前元素 $b$ 与 $a$ 的差值大于 $k$,则更新 $a$ 为 $b$,并将子序列数目加 1。遍历结束后,即可得到最少子序列数目,注意初始时子序列数目为 $1$。
8787

88-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
88+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8989

9090
<!-- tabs:start -->
9191

solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ Since three subsequences were created, we return 3. It can be shown that 3 is th
7979

8080
<!-- solution:start -->
8181

82-
### Solution 1
82+
### Solution 1: Greedy + Sorting
83+
84+
The problem requires dividing into subsequences, not subarrays, so the elements in a subsequence can be non-continuous. We can sort the array $\textit{nums}$. Assuming the first element of the current subsequence is $a$, the difference between the maximum and minimum values in the subsequence will not exceed $k$. Therefore, we can iterate through the array $\textit{nums}$. If the difference between the current element $b$ and $a$ is greater than $k$, then update $a$ to $b$ and increase the number of subsequences by 1. After the iteration, we can obtain the minimum number of subsequences, noting that the initial number of subsequences is $1$.
85+
86+
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 $\textit{nums}$.
8387

8488
<!-- tabs:start -->
8589

solution/2200-2299/2295.Replace Elements in an Array/README.md

+25-24
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ tags:
7878

7979
### 方法一:哈希表
8080

81-
我们先用哈希表 $d$ 记录数组 `nums` 中每个数字的下标,然后遍历操作数组 `operations`,对于每个操作 $[a, b]$,我们将 $a$ 在 `nums` 中的下标 $d[a]$ 对应的数字替换为 $b$,并更新 $d$ 中 $b$ 的下标为 $d[a]$。
81+
我们先用哈希表 $d$ 记录数组 $\textit{nums}$ 中每个数字的下标,然后遍历操作数组 $\textit{operations}$,对于每个操作 $[x, y]$,我们将 $x$ 在 $\textit{nums}$ 中的下标 $d[x]$ 对应的数字替换为 $y$,并更新 $d$ 中 $y$ 的下标为 $d[x]$。
8282

83-
最后返回 `nums` 即可。
83+
最后返回 $\textit{nums}$ 即可。
8484

85-
时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 `nums``operations` 的长度。
85+
时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $\textit{nums}$ 的长度和操作数组 $\textit{operations}$ 的长度。
8686

8787
<!-- tabs:start -->
8888

@@ -91,10 +91,10 @@ tags:
9191
```python
9292
class Solution:
9393
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
94-
d = {v: i for i, v in enumerate(nums)}
95-
for a, b in operations:
96-
nums[d[a]] = b
97-
d[b] = d[a]
94+
d = {x: i for i, x in enumerate(nums)}
95+
for x, y in operations:
96+
nums[d[x]] = y
97+
d[y] = d[x]
9898
return nums
9999
```
100100

@@ -103,14 +103,15 @@ class Solution:
103103
```java
104104
class Solution {
105105
public int[] arrayChange(int[] nums, int[][] operations) {
106-
Map<Integer, Integer> d = new HashMap<>();
107-
for (int i = 0; i < nums.length; ++i) {
106+
int n = nums.length;
107+
Map<Integer, Integer> d = new HashMap<>(n);
108+
for (int i = 0; i < n; ++i) {
108109
d.put(nums[i], i);
109110
}
110111
for (var op : operations) {
111-
int a = op[0], b = op[1];
112-
nums[d.get(a)] = b;
113-
d.put(b, d.get(a));
112+
int x = op[0], y = op[1];
113+
nums[d.get(x)] = y;
114+
d.put(y, d.get(x));
114115
}
115116
return nums;
116117
}
@@ -128,9 +129,9 @@ public:
128129
d[nums[i]] = i;
129130
}
130131
for (auto& op : operations) {
131-
int a = op[0], b = op[1];
132-
nums[d[a]] = b;
133-
d[b] = d[a];
132+
int x = op[0], y = op[1];
133+
nums[d[x]] = y;
134+
d[y] = d[x];
134135
}
135136
return nums;
136137
}
@@ -142,13 +143,13 @@ public:
142143
```go
143144
func arrayChange(nums []int, operations [][]int) []int {
144145
d := map[int]int{}
145-
for i, v := range nums {
146-
d[v] = i
146+
for i, x := range nums {
147+
d[x] = i
147148
}
148149
for _, op := range operations {
149-
a, b := op[0], op[1]
150-
nums[d[a]] = b
151-
d[b] = d[a]
150+
x, y := op[0], op[1]
151+
nums[d[x]] = y
152+
d[y] = d[x]
152153
}
153154
return nums
154155
}
@@ -158,10 +159,10 @@ func arrayChange(nums []int, operations [][]int) []int {
158159

159160
```ts
160161
function arrayChange(nums: number[], operations: number[][]): number[] {
161-
const d = new Map(nums.map((v, i) => [v, i]));
162-
for (const [a, b] of operations) {
163-
nums[d.get(a)] = b;
164-
d.set(b, d.get(a));
162+
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
163+
for (const [x, y] of operations) {
164+
nums[d.get(x)!] = y;
165+
d.set(y, d.get(x)!);
165166
}
166167
return nums;
167168
}

solution/2200-2299/2295.Replace Elements in an Array/README_EN.md

+25-24
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ We return the array [2,1].
7878

7979
### Solution 1: Hash Table
8080

81-
First, we use a hash table $d$ to record the index of each number in the array `nums`. Then, we iterate through the operation array `operations`. For each operation $[a, b]$, we replace the number at index $d[a]$ in `nums` with $b$, and update the index of $b$ in $d$ to $d[a]$.
81+
First, we use a hash table $d$ to record the indices of each number in the array $\textit{nums}$. Then, we iterate through the operation array $\textit{operations}$. For each operation $[x, y]$, we replace the number at index $d[x]$ in $\textit{nums}$ with $y$, and update the index of $y$ in $d$ to $d[x]$.
8282

83-
Finally, we return `nums`.
83+
Finally, we return $\textit{nums}$.
8484

85-
The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the arrays `nums` and `operations`, respectively.
85+
The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the array $\textit{nums}$ and the operation array $\textit{operations}$, respectively.
8686

8787
<!-- tabs:start -->
8888

@@ -91,10 +91,10 @@ The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$
9191
```python
9292
class Solution:
9393
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
94-
d = {v: i for i, v in enumerate(nums)}
95-
for a, b in operations:
96-
nums[d[a]] = b
97-
d[b] = d[a]
94+
d = {x: i for i, x in enumerate(nums)}
95+
for x, y in operations:
96+
nums[d[x]] = y
97+
d[y] = d[x]
9898
return nums
9999
```
100100

@@ -103,14 +103,15 @@ class Solution:
103103
```java
104104
class Solution {
105105
public int[] arrayChange(int[] nums, int[][] operations) {
106-
Map<Integer, Integer> d = new HashMap<>();
107-
for (int i = 0; i < nums.length; ++i) {
106+
int n = nums.length;
107+
Map<Integer, Integer> d = new HashMap<>(n);
108+
for (int i = 0; i < n; ++i) {
108109
d.put(nums[i], i);
109110
}
110111
for (var op : operations) {
111-
int a = op[0], b = op[1];
112-
nums[d.get(a)] = b;
113-
d.put(b, d.get(a));
112+
int x = op[0], y = op[1];
113+
nums[d.get(x)] = y;
114+
d.put(y, d.get(x));
114115
}
115116
return nums;
116117
}
@@ -128,9 +129,9 @@ public:
128129
d[nums[i]] = i;
129130
}
130131
for (auto& op : operations) {
131-
int a = op[0], b = op[1];
132-
nums[d[a]] = b;
133-
d[b] = d[a];
132+
int x = op[0], y = op[1];
133+
nums[d[x]] = y;
134+
d[y] = d[x];
134135
}
135136
return nums;
136137
}
@@ -142,13 +143,13 @@ public:
142143
```go
143144
func arrayChange(nums []int, operations [][]int) []int {
144145
d := map[int]int{}
145-
for i, v := range nums {
146-
d[v] = i
146+
for i, x := range nums {
147+
d[x] = i
147148
}
148149
for _, op := range operations {
149-
a, b := op[0], op[1]
150-
nums[d[a]] = b
151-
d[b] = d[a]
150+
x, y := op[0], op[1]
151+
nums[d[x]] = y
152+
d[y] = d[x]
152153
}
153154
return nums
154155
}
@@ -158,10 +159,10 @@ func arrayChange(nums []int, operations [][]int) []int {
158159

159160
```ts
160161
function arrayChange(nums: number[], operations: number[][]): number[] {
161-
const d = new Map(nums.map((v, i) => [v, i]));
162-
for (const [a, b] of operations) {
163-
nums[d.get(a)] = b;
164-
d.set(b, d.get(a));
162+
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
163+
for (const [x, y] of operations) {
164+
nums[d.get(x)!] = y;
165+
d.set(y, d.get(x)!);
165166
}
166167
return nums;
167168
}

solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class Solution {
66
d[nums[i]] = i;
77
}
88
for (auto& op : operations) {
9-
int a = op[0], b = op[1];
10-
nums[d[a]] = b;
11-
d[b] = d[a];
9+
int x = op[0], y = op[1];
10+
nums[d[x]] = y;
11+
d[y] = d[x];
1212
}
1313
return nums;
1414
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func arrayChange(nums []int, operations [][]int) []int {
22
d := map[int]int{}
3-
for i, v := range nums {
4-
d[v] = i
3+
for i, x := range nums {
4+
d[x] = i
55
}
66
for _, op := range operations {
7-
a, b := op[0], op[1]
8-
nums[d[a]] = b
9-
d[b] = d[a]
7+
x, y := op[0], op[1]
8+
nums[d[x]] = y
9+
d[y] = d[x]
1010
}
1111
return nums
1212
}

solution/2200-2299/2295.Replace Elements in an Array/Solution.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
public int[] arrayChange(int[] nums, int[][] operations) {
3-
Map<Integer, Integer> d = new HashMap<>();
4-
for (int i = 0; i < nums.length; ++i) {
3+
int n = nums.length;
4+
Map<Integer, Integer> d = new HashMap<>(n);
5+
for (int i = 0; i < n; ++i) {
56
d.put(nums[i], i);
67
}
78
for (var op : operations) {
8-
int a = op[0], b = op[1];
9-
nums[d.get(a)] = b;
10-
d.put(b, d.get(a));
9+
int x = op[0], y = op[1];
10+
nums[d.get(x)] = y;
11+
d.put(y, d.get(x));
1112
}
1213
return nums;
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
3-
d = {v: i for i, v in enumerate(nums)}
4-
for a, b in operations:
5-
nums[d[a]] = b
6-
d[b] = d[a]
3+
d = {x: i for i, x in enumerate(nums)}
4+
for x, y in operations:
5+
nums[d[x]] = y
6+
d[y] = d[x]
77
return nums
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function arrayChange(nums: number[], operations: number[][]): number[] {
2-
const d = new Map(nums.map((v, i) => [v, i]));
3-
for (const [a, b] of operations) {
4-
nums[d.get(a)] = b;
5-
d.set(b, d.get(a));
2+
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
3+
for (const [x, y] of operations) {
4+
nums[d.get(x)!] = y;
5+
d.set(y, d.get(x)!);
66
}
77
return nums;
88
}

0 commit comments

Comments
 (0)