Loading

LeetCode problem#1920

Build Array from Permutation

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

39 lines
1class Solution
2{
3    int[] buildArray(int[] nums)
4    {
5        int[] arr = new int[nums.length];
6
7        for (int i = nums.length; i > 0; --i)
8        {
9            arr[i - 1] = nums[nums[i - 1]];
10        }
11
12        return arr;
13    }
14
15    void test()
16    {
17        int[][] testInputs = {
18            {0, 2, 1, 5, 3, 4},
19            {0, 1, 2, 4, 5, 3}
20        };
21        int[][] testOutputs = {
22            {0, 1, 2, 4, 5, 3},
23            {4, 5, 0, 1, 2, 3}
24        };
25        for (int i = 0; i < testInputs.length; i++) {
26            int[] testInput = testInputs[i];
27            int[] testOutput = testOutputs[i];
28            int[] testcase = buildArray(testInput);
29            for (int j = 0; j < testInput.length; j++) {
30                assert testcase[j] == testOutput[j];
31            }
32        }
33    }
34
35    public static void main(String[] args)
36    {
37        new Solution().test();
38    }
39}

How it works

The provided code is a solution to a common problem on platforms like LeetCode, where you want to transform an input array based on a certain mapping. Let's break down the solution:

Problem Description:

The problem typically asks you to take an array of integers, nums, where each integer is an index, and build a new array, arr, such that:

  • Each element in arr at index i is equal to the value located at the index given by nums[i].

Code Explanation:

The class Solution contains two methods:

1. buildArray Method:

This is the main method that performs the transformation:

1int[] buildArray(int[] nums) {
2    int[] arr = new int[nums.length];  // Create a new array to hold the result
3
4    for (int i = nums.length; i > 0; --i) {  // Iterate backwards through the original array
5        arr[i - 1] = nums[nums[i - 1]];  // Set each element in the new array using the mapping
6    }
7
8    return arr;  // Return the transformed array
9}
  • It first initializes a new array arr with the same length as nums.
  • It then iterates through the array nums in reverse using for (int i = nums.length; i > 0; --i).
  • For each index i-1 of arr, it assigns the value at nums[nums[i-1]], which means it first looks up nums[i-1] to get an index, and then uses that index to get a value from nums to populate arr[i-1].
  • After processing all elements, it returns the newly created array arr.

The choice of iterating backwards (from nums.length down to 1) is a stylistic choice here; the loop could have also been written to iterate forwards without affecting correctness.

2. test Method:

This method is for testing the functionality of buildArray:

1void test() {
2    int[][] testInputs = {
3        {0, 2, 1, 5, 3, 4},
4        {0, 1, 2, 4, 5, 3}
5    };
6    int[][] testOutputs = {
7        {0, 1, 2, 4, 5, 3},
8        {4, 5, 0, 1, 2, 3}
9    };
10    for (int i = 0; i < testInputs.length; i++) {
11        int[] testInput = testInputs[i];
12        int[] testOutput = testOutputs[i];
13        int[] testcase = buildArray(testInput);  // Call buildArray with the test input
14        for (int j = 0; j < testInput.length; j++) {
15            assert testcase[j] == testOutput[j];  // Verify the output matches the expected output
16        }
17    }
18}
  • Here, testInputs contains various input arrays that you want to test.
  • testOutputs contains the expected results for the corresponding inputs.
  • The for loop iterates through each test case, passing the test input to buildArray and comparing the returned result with the expected output.
  • The assert statement checks if the computed array matches the expected one; if it doesn't, the program will throw an assertion error.

3. main Method:

This is the entry point of the program:

public static void main(String[] args) {
    new Solution().test();  // Create an instance of Solution and run the tests
}
  • It creates an instance of Solution and calls the test method to run all the defined test cases.

Summary:

In summary, this Java class provides a solution to transform an input array based on index mapping and includes a testing mechanism to validate its correctness. The method efficiently constructs the new array using the original indices provided in the nums array.

Written by AI from the code above, so double-check anything important.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.