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 problems: No.2293~2295 #3285

Merged
merged 1 commit into from
Jul 19, 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
2 changes: 1 addition & 1 deletion solution/2200-2299/2293.Min Max Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down
6 changes: 5 additions & 1 deletion solution/2200-2299/2293.Min Max Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ Third: nums = [1]

<!-- solution:start -->

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

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.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ tags:

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ Since three subsequences were created, we return 3. It can be shown that 3 is th

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Sorting

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

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

<!-- tabs:start -->

Expand Down
49 changes: 25 additions & 24 deletions solution/2200-2299/2295.Replace Elements in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ tags:

### 方法一:哈希表

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

最后返回 `nums` 即可。
最后返回 $\textit{nums}$ 即可。

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

<!-- tabs:start -->

Expand All @@ -91,10 +91,10 @@ tags:
```python
class Solution:
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
d = {v: i for i, v in enumerate(nums)}
for a, b in operations:
nums[d[a]] = b
d[b] = d[a]
d = {x: i for i, x in enumerate(nums)}
for x, y in operations:
nums[d[x]] = y
d[y] = d[x]
return nums
```

Expand All @@ -103,14 +103,15 @@ class Solution:
```java
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> d = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
int n = nums.length;
Map<Integer, Integer> d = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
d.put(nums[i], i);
}
for (var op : operations) {
int a = op[0], b = op[1];
nums[d.get(a)] = b;
d.put(b, d.get(a));
int x = op[0], y = op[1];
nums[d.get(x)] = y;
d.put(y, d.get(x));
}
return nums;
}
Expand All @@ -128,9 +129,9 @@ public:
d[nums[i]] = i;
}
for (auto& op : operations) {
int a = op[0], b = op[1];
nums[d[a]] = b;
d[b] = d[a];
int x = op[0], y = op[1];
nums[d[x]] = y;
d[y] = d[x];
}
return nums;
}
Expand All @@ -142,13 +143,13 @@ public:
```go
func arrayChange(nums []int, operations [][]int) []int {
d := map[int]int{}
for i, v := range nums {
d[v] = i
for i, x := range nums {
d[x] = i
}
for _, op := range operations {
a, b := op[0], op[1]
nums[d[a]] = b
d[b] = d[a]
x, y := op[0], op[1]
nums[d[x]] = y
d[y] = d[x]
}
return nums
}
Expand All @@ -158,10 +159,10 @@ func arrayChange(nums []int, operations [][]int) []int {

```ts
function arrayChange(nums: number[], operations: number[][]): number[] {
const d = new Map(nums.map((v, i) => [v, i]));
for (const [a, b] of operations) {
nums[d.get(a)] = b;
d.set(b, d.get(a));
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
for (const [x, y] of operations) {
nums[d.get(x)!] = y;
d.set(y, d.get(x)!);
}
return nums;
}
Expand Down
49 changes: 25 additions & 24 deletions solution/2200-2299/2295.Replace Elements in an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ We return the array [2,1].

### Solution 1: Hash Table

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

Finally, we return `nums`.
Finally, we return $\textit{nums}$.

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

<!-- tabs:start -->

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

Expand All @@ -103,14 +103,15 @@ class Solution:
```java
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> d = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
int n = nums.length;
Map<Integer, Integer> d = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
d.put(nums[i], i);
}
for (var op : operations) {
int a = op[0], b = op[1];
nums[d.get(a)] = b;
d.put(b, d.get(a));
int x = op[0], y = op[1];
nums[d.get(x)] = y;
d.put(y, d.get(x));
}
return nums;
}
Expand All @@ -128,9 +129,9 @@ public:
d[nums[i]] = i;
}
for (auto& op : operations) {
int a = op[0], b = op[1];
nums[d[a]] = b;
d[b] = d[a];
int x = op[0], y = op[1];
nums[d[x]] = y;
d[y] = d[x];
}
return nums;
}
Expand All @@ -142,13 +143,13 @@ public:
```go
func arrayChange(nums []int, operations [][]int) []int {
d := map[int]int{}
for i, v := range nums {
d[v] = i
for i, x := range nums {
d[x] = i
}
for _, op := range operations {
a, b := op[0], op[1]
nums[d[a]] = b
d[b] = d[a]
x, y := op[0], op[1]
nums[d[x]] = y
d[y] = d[x]
}
return nums
}
Expand All @@ -158,10 +159,10 @@ func arrayChange(nums []int, operations [][]int) []int {

```ts
function arrayChange(nums: number[], operations: number[][]): number[] {
const d = new Map(nums.map((v, i) => [v, i]));
for (const [a, b] of operations) {
nums[d.get(a)] = b;
d.set(b, d.get(a));
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
for (const [x, y] of operations) {
nums[d.get(x)!] = y;
d.set(y, d.get(x)!);
}
return nums;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Solution {
d[nums[i]] = i;
}
for (auto& op : operations) {
int a = op[0], b = op[1];
nums[d[a]] = b;
d[b] = d[a];
int x = op[0], y = op[1];
nums[d[x]] = y;
d[y] = d[x];
}
return nums;
}
Expand Down
10 changes: 5 additions & 5 deletions solution/2200-2299/2295.Replace Elements in an Array/Solution.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
func arrayChange(nums []int, operations [][]int) []int {
d := map[int]int{}
for i, v := range nums {
d[v] = i
for i, x := range nums {
d[x] = i
}
for _, op := range operations {
a, b := op[0], op[1]
nums[d[a]] = b
d[b] = d[a]
x, y := op[0], op[1]
nums[d[x]] = y
d[y] = d[x]
}
return nums
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> d = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
int n = nums.length;
Map<Integer, Integer> d = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
d.put(nums[i], i);
}
for (var op : operations) {
int a = op[0], b = op[1];
nums[d.get(a)] = b;
d.put(b, d.get(a));
int x = op[0], y = op[1];
nums[d.get(x)] = y;
d.put(y, d.get(x));
}
return nums;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
d = {v: i for i, v in enumerate(nums)}
for a, b in operations:
nums[d[a]] = b
d[b] = d[a]
d = {x: i for i, x in enumerate(nums)}
for x, y in operations:
nums[d[x]] = y
d[y] = d[x]
return nums
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function arrayChange(nums: number[], operations: number[][]): number[] {
const d = new Map(nums.map((v, i) => [v, i]));
for (const [a, b] of operations) {
nums[d.get(a)] = b;
d.set(b, d.get(a));
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
for (const [x, y] of operations) {
nums[d.get(x)!] = y;
d.set(y, d.get(x)!);
}
return nums;
}
Loading