1920. Build Array from Permutation
Difficulty: Easy
Topics: Array, Simulation
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 1:
- Input: nums = [0,2,1,5,3,4]
- Output: [0,1,2,4,5,3]
- Explanation: 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]
Example 2:
- Input: nums = [5,0,1,2,3,4]
- Output: [4,5,0,1,2,3]
- Explanation: 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[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
= [4,5,0,1,2,3]
Constraints:
1 <= nums.length <= 10000 <= nums[i] < nums.length- The elements in
numsare distinct.
Hint:
- Just apply what's said in the statement.
- Notice that you can't apply it on the same array directly since some elements will change after application
Solution:
We need to build an array ans from a given zero-based permutation nums such that each element ans[i] is equal to nums[nums[i]]. The solution involves iterating through the input array and constructing the result array using the specified transformation.
Approach
-
Problem Analysis: The problem requires transforming each element of the input array
numssuch that each element in the resulting arrayansat indexiis the element innumsat the index specified by the element innumsat indexi. This can be succinctly expressed asans[i] = nums[nums[i]]. - Intuition: Since the input array is a zero-based permutation, each element is unique and within the valid index range. This allows us to directly use each element as an index to access another element in the same array without worrying about out-of-bounds errors.
-
Algorithm:
- Initialize an empty array
ansof the same length asnums. - Iterate over each index
iin the input arraynums. - For each index
i, computeans[i]asnums[nums[i]].
- Initialize an empty array
- Complexity: The time complexity is O(n) where n is the length of the input array, as we iterate through the array once. The space complexity is O(n) as well, due to the storage required for the result array.
Let's implement this solution in PHP: 1920. Build Array from Permutation
<?php
/**
* @param Integer[] $nums
* @return Integer[]
*/
function buildArray($nums) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test case 1
$nums1 = array(0, 2, 1, 5, 3, 4);
print_r(buildArray($nums1)); // Output: [0, 1, 2, 4, 5, 3]
// Test case 2
$nums2 = array(5, 0, 1, 2, 3, 4);
print_r(buildArray($nums2)); // Output: [4, 5, 0, 1, 2, 3]
?>
Explanation:
-
Initialization: We start by creating an empty array
ansto store the result. -
Iteration: We loop through each index
ifrom 0 to the length ofnumsminus 1. -
Transformation: For each index
i, we setans[i]to the value ofnumsat the index specified bynums[i]. This effectively applies the transformationans[i] = nums[nums[i]]. -
Return: Finally, we return the constructed array
ans.
This approach efficiently constructs the result array in linear time, ensuring that each element is transformed correctly according to the problem's requirements.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)