EasyNo. 1920
Build Array from Permutation — LeetCode 1920 Python Solution
- Topics
- Array, Simulation
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it. A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
Example
- Input
- nums = [0,2,1,5,3,4]
- Output
- [0,1,2,4,5,3]
- Explanation
- The array ans is built as follows:
Python solution
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
return [nums[num] for num in nums]Complexity
- Time
- O(n)where n is the length of the array nums
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1929 | Concatenation of Array | Easy |
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1503 | Last Moment Before All Ants Fall Out of a Plank | Medium |
Frequently asked questions
- How hard is LeetCode 1920. Build Array from Permutation?
LeetCode 1920. Build Array from Permutation is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1920. Build Array from Permutation?
The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.
- What is the space complexity of LeetCode 1920. Build Array from Permutation?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1920. Build Array from Permutation cover?
LeetCode 1920. Build Array from Permutation is tagged Array and Simulation on LeetCode.