Skip to content

Instantly share code, notes, and snippets.

@nerdyalgorithm
Last active August 5, 2025 19:20
Show Gist options
  • Select an option

  • Save nerdyalgorithm/4475b3ce28460b376864f39426f940d8 to your computer and use it in GitHub Desktop.

Select an option

Save nerdyalgorithm/4475b3ce28460b376864f39426f940d8 to your computer and use it in GitHub Desktop.
LeetCode 1920 — Build Array from Permutation

QUESTION

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.

APPROACH

The problem says to return an array where ans[i] = nums[nums[i]]. So for every index i, I just needed to find nums[i], then use that result as a new index to get the final value.

Since nums is a 0-based permutation (values from 0 to n−1).

IMPLEMENTATION

 Solution:
    def buildArray(self, nums: List[int]) -> List[int]:
        return [nums[nums[i]] for i in range(len(nums))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment