HomeLeetcode1920. Build Array from Permutation - Solutions

1920. Build Array from Permutation – Solutions

Description:

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

Solution in Python:

To solve this problem, you need to create a new array ans such that each element ans[i] is equal to nums[nums[i]] for all valid indices i. Given the constraints and properties of nums, this problem can be solved straightforwardly with an O(n) time complexity using an additional array to store the results.

class Solution(object):
    def buildArray(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        # Get the length of the input array nums
        n = len(nums)
        
        # Initialize the result array 'ans' with the same length as 'nums'
        # Each element is initially set to 0
        ans = [0] * n
        
        # Iterate through each index 'i' in the range from 0 to n-1
        for i in range(n):
            # Set ans[i] to be the element at the index specified by nums[i]
            # This means ans[i] = nums[nums[i]]
            ans[i] = nums[nums[i]]
        
        # Return the constructed result array 'ans'
        return ans
Python

This method constructs a new array ans such that each element ans[i] is assigned the value of nums[nums[i]] based on the input array nums. The method efficiently iterates through the indices of nums, performs the necessary lookup, and builds the result array, which is then returned.

Solution in Javascript:

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var buildArray = function(nums) {
    // Get the length of the input array nums
    let n = nums.length;
    
    // Initialize the result array 'ans' with the same length as 'nums'
    // Each element is initially set to 0
    let ans = new Array(n).fill(0);
    
    // Iterate through each index 'i' in the range from 0 to n-1
    for (let i = 0; i < n; i++) {
        // Set ans[i] to be the element at the index specified by nums[i]
        // This means ans[i] = nums[nums[i]]
        ans[i] = nums[nums[i]];
    }
    
    // Return the constructed result array 'ans'
    return ans;
};

// Example usage:
console.log(buildArray([0, 2, 1, 5, 3, 4]));  // Output: [0, 1, 2, 4, 5, 3]
console.log(buildArray([5, 0, 1, 2, 3, 4]));  // Output: [4, 5, 0, 1, 2, 3]
JavaScript

Solution in Java:

public class Solution {
    public int[] buildArray(int[] nums) {
        // Get the length of the input array nums
        int n = nums.length;
        
        // Initialize the result array 'ans' with the same length as 'nums'
        int[] ans = new int[n];
        
        // Iterate through each index 'i' in the range from 0 to n-1
        for (int i = 0; i < n; i++) {
            // Set ans[i] to be the element at the index specified by nums[i]
            // This means ans[i] = nums[nums[i]]
            ans[i] = nums[nums[i]];
        }
        
        // Return the constructed result array 'ans'
        return ans;
    }

    // Example usage
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {0, 2, 1, 5, 3, 4};
        int[] result1 = solution.buildArray(nums1);
        for (int num : result1) {
            System.out.print(num + " ");
        }
        System.out.println(); // Output: 0 1 2 4 5 3
        
        int[] nums2 = {5, 0, 1, 2, 3, 4};
        int[] result2 = solution.buildArray(nums2);
        for (int num : result2) {
            System.out.print(num + " ");
        }
        System.out.println(); // Output: 4 5 0 1 2 3
    }
}
Java

Solution in C#:

public class Solution {
    public int[] BuildArray(int[] nums) {
        // Get the length of the input array nums
        int n = nums.Length;
        
        // Initialize the result array 'ans' with the same length as 'nums'
        int[] ans = new int[n];
        
        // Iterate through each index 'i' in the range from 0 to n-1
        for (int i = 0; i < n; i++) {
            // Set ans[i] to be the element at the index specified by nums[i]
            // This means ans[i] = nums[nums[i]]
            ans[i] = nums[nums[i]];
        }
        
        // Return the constructed result array 'ans'
        return ans;
    }
}
C#

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular