Build Array from Permutation — LeetCode 1920 Python Solution
EasyArraySimulation
- Problem
- #1920
- 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
Python
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
return [nums[num] for num in nums]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array \textit{nums} |
| Space | O(1) auxiliary |
Related problems
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.