Skip to content

Commit 4b836c8

Browse files
authored
feat: add solutions to lc problem: No.1920 (#4032)
No.1920.Build Array from Permutation
1 parent 59cb267 commit 4b836c8

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

solution/1900-1999/1920.Build Array from Permutation/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]]
6565

6666
<!-- solution:start -->
6767

68-
### 方法一
68+
### 方法一:模拟
69+
70+
我们可以直接模拟题目描述的过程,构建一个新的数组 $\textit{ans}$,对于每个 $i$,令 $\textit{ans}[i] = \textit{nums}[\textit{nums}[i]]$。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
6973

7074
<!-- tabs:start -->
7175

@@ -122,7 +126,7 @@ func buildArray(nums []int) []int {
122126

123127
```ts
124128
function buildArray(nums: number[]): number[] {
125-
return nums.map(v => nums[v]);
129+
return nums.map(x => nums[x]);
126130
}
127131
```
128132

@@ -144,11 +148,7 @@ impl Solution {
144148
* @return {number[]}
145149
*/
146150
var buildArray = function (nums) {
147-
let ans = [];
148-
for (let i = 0; i < nums.length; ++i) {
149-
ans[i] = nums[nums[i]];
150-
}
151-
return ans;
151+
return nums.map(x => nums[x]);
152152
};
153153
```
154154

solution/1900-1999/1920.Build Array from Permutation/README_EN.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929
<pre>
3030
<strong>Input:</strong> nums = [0,2,1,5,3,4]
3131
<strong>Output:</strong> [0,1,2,4,5,3]<strong>
32-
Explanation:</strong> The array ans is built as follows:
32+
Explanation:</strong> The array ans is built as follows:
3333
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
3434
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
3535
= [0,1,2,4,5,3]</pre>
@@ -62,7 +62,11 @@ ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]]
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Simulation
66+
67+
We can directly simulate the process described in the problem by constructing a new array $\textit{ans}$. For each $i$, let $\textit{ans}[i] = \textit{nums}[\textit{nums}[i]]$.
68+
69+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
6670

6771
<!-- tabs:start -->
6872

@@ -119,7 +123,7 @@ func buildArray(nums []int) []int {
119123

120124
```ts
121125
function buildArray(nums: number[]): number[] {
122-
return nums.map(v => nums[v]);
126+
return nums.map(x => nums[x]);
123127
}
124128
```
125129

@@ -141,11 +145,7 @@ impl Solution {
141145
* @return {number[]}
142146
*/
143147
var buildArray = function (nums) {
144-
let ans = [];
145-
for (let i = 0; i < nums.length; ++i) {
146-
ans[i] = nums[nums[i]];
147-
}
148-
return ans;
148+
return nums.map(x => nums[x]);
149149
};
150150
```
151151

solution/1900-1999/1920.Build Array from Permutation/Solution.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
* @return {number[]}
44
*/
55
var buildArray = function (nums) {
6-
let ans = [];
7-
for (let i = 0; i < nums.length; ++i) {
8-
ans[i] = nums[nums[i]];
9-
}
10-
return ans;
6+
return nums.map(x => nums[x]);
117
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function buildArray(nums: number[]): number[] {
2-
return nums.map(v => nums[v]);
2+
return nums.map(x => nums[x]);
33
}

solution/1900-1999/1996.The Number of Weak Characters in the Game/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ tags:
6868

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

71-
### Solution 1
71+
### Solution 1: Sorting + Traversal
72+
73+
We can sort all characters in descending order of attack power and ascending order of defense power.
74+
75+
Then, traverse all characters. For the current character, if its defense power is less than the previous maximum defense power, it is a weak character, and we increment the answer by one. Otherwise, update the maximum defense power.
76+
77+
After the traversal, we get the answer.
78+
79+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of characters.
7280

7381
<!-- tabs:start -->
7482

0 commit comments

Comments
 (0)