diff --git a/solution/2200-2299/2293.Min Max Game/README.md b/solution/2200-2299/2293.Min Max Game/README.md index 4f7e26fcfc8f0..dfd9109d849fa 100644 --- a/solution/2200-2299/2293.Min Max Game/README.md +++ b/solution/2200-2299/2293.Min Max Game/README.md @@ -77,7 +77,7 @@ tags: 根据题意,我们可以模拟整个过程,最后剩下的数字即为答案。在实现上,我们不需要额外创建数组,直接在原数组上进行操作即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/2200-2299/2293.Min Max Game/README_EN.md b/solution/2200-2299/2293.Min Max Game/README_EN.md index 4591bb10b2c62..f78dee5b0e82a 100644 --- a/solution/2200-2299/2293.Min Max Game/README_EN.md +++ b/solution/2200-2299/2293.Min Max Game/README_EN.md @@ -69,7 +69,11 @@ Third: nums = [1] -### 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)$. diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md index bbcd842bc17c7..69b53d410c102 100644 --- a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md @@ -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}$ 的长度。 diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md index bbf4c0002ffcb..3b4c283347c28 100644 --- a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md @@ -79,7 +79,11 @@ Since three subsequences were created, we return 3. It can be shown that 3 is th -### 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}$. diff --git a/solution/2200-2299/2295.Replace Elements in an Array/README.md b/solution/2200-2299/2295.Replace Elements in an Array/README.md index 3509330051a05..81ccb2e8f4937 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/README.md +++ b/solution/2200-2299/2295.Replace Elements in an Array/README.md @@ -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}$ 的长度。 @@ -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 ``` @@ -103,14 +103,15 @@ class Solution: ```java class Solution { public int[] arrayChange(int[] nums, int[][] operations) { - Map d = new HashMap<>(); - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + Map 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; } @@ -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; } @@ -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 } @@ -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 = 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; } diff --git a/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md b/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md index 8ed95085bc8b5..a09cb860bfc12 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md +++ b/solution/2200-2299/2295.Replace Elements in an Array/README_EN.md @@ -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. @@ -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 ``` @@ -103,14 +103,15 @@ class Solution: ```java class Solution { public int[] arrayChange(int[] nums, int[][] operations) { - Map d = new HashMap<>(); - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + Map 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; } @@ -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; } @@ -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 } @@ -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 = 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; } diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp b/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp index 6f6be5c0e7745..71b95a9b7b275 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp @@ -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; } diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.go b/solution/2200-2299/2295.Replace Elements in an Array/Solution.go index c4ff655230e6b..c043ebc8c9226 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.go +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.go @@ -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 } \ No newline at end of file diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.java b/solution/2200-2299/2295.Replace Elements in an Array/Solution.java index c17fe88d53930..0ad2c0e567ba2 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.java +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.java @@ -1,13 +1,14 @@ class Solution { public int[] arrayChange(int[] nums, int[][] operations) { - Map d = new HashMap<>(); - for (int i = 0; i < nums.length; ++i) { + int n = nums.length; + Map 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; } diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.py b/solution/2200-2299/2295.Replace Elements in an Array/Solution.py index cc9ef4fe10e47..57097e05cdc41 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.py +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.py @@ -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 diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.ts b/solution/2200-2299/2295.Replace Elements in an Array/Solution.ts index 1a0503287b428..37a87013c6c91 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.ts +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.ts @@ -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 = 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; }