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: add solutions to lc problem: No.1920 #4032

Merged
merged 1 commit into from
Feb 6, 2025
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: 7 additions & 7 deletions solution/1900-1999/1920.Build Array from Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]]

<!-- solution:start -->

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

我们可以直接模拟题目描述的过程,构建一个新的数组 $\textit{ans}$,对于每个 $i$,令 $\textit{ans}[i] = \textit{nums}[\textit{nums}[i]]$。

时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

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

```ts
function buildArray(nums: number[]): number[] {
return nums.map(v => nums[v]);
return nums.map(x => nums[x]);
}
```

Expand All @@ -144,11 +148,7 @@ impl Solution {
* @return {number[]}
*/
var buildArray = function (nums) {
let ans = [];
for (let i = 0; i < nums.length; ++i) {
ans[i] = nums[nums[i]];
}
return ans;
return nums.map(x => nums[x]);
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [0,2,1,5,3,4]
<strong>Output:</strong> [0,1,2,4,5,3]<strong>
Explanation:</strong> The array ans is built as follows:
Explanation:</strong> The array ans is built as follows:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
= [0,1,2,4,5,3]</pre>
Expand Down Expand Up @@ -62,7 +62,11 @@ ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]]

<!-- solution:start -->

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

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

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

<!-- tabs:start -->

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

```ts
function buildArray(nums: number[]): number[] {
return nums.map(v => nums[v]);
return nums.map(x => nums[x]);
}
```

Expand All @@ -141,11 +145,7 @@ impl Solution {
* @return {number[]}
*/
var buildArray = function (nums) {
let ans = [];
for (let i = 0; i < nums.length; ++i) {
ans[i] = nums[nums[i]];
}
return ans;
return nums.map(x => nums[x]);
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
* @return {number[]}
*/
var buildArray = function (nums) {
let ans = [];
for (let i = 0; i < nums.length; ++i) {
ans[i] = nums[nums[i]];
}
return ans;
return nums.map(x => nums[x]);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function buildArray(nums: number[]): number[] {
return nums.map(v => nums[v]);
return nums.map(x => nums[x]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Traversal

We can sort all characters in descending order of attack power and ascending order of defense power.

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.

After the traversal, we get the answer.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of characters.

<!-- tabs:start -->

Expand Down