Given two arrays of integers nums
and index
. Your task is to create target array under the following rules:
index[i]
the value nums[i]
in target array.nums
and index.
Return the target array.
It is guaranteed that the insertion operations will be valid.
Example 1:
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1] Output: [0,4,1,3,2] Explanation: nums index target 0 0 [0] 1 1 [0,1] 2 2 [0,1,2] 3 2 [0,1,3,2] 4 1 [0,4,1,3,2]
Example 2:
Input: nums = [1,2,3,4,0], index = [0,1,2,3,0] Output: [0,1,2,3,4] Explanation: nums index target 1 0 [1] 2 1 [1,2] 3 2 [1,2,3] 4 3 [1,2,3,4] 0 0 [0,1,2,3,4]
Example 3:
Input: nums = [1], index = [0] Output: [1]
Constraints:
1 <= nums.length, index.length <= 100
nums.length == index.length
0 <= nums[i] <= 100
0 <= index[i] <= i
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're building a new list by inserting numbers from an existing list into specific spots. A simple, but potentially slow, way to do this is to insert each number one at a time, shifting the other numbers as needed to make space.
Here's how the algorithm would work step-by-step:
def create_target_array(numbers, indices): target_array = []
for i in range(len(numbers)): index_to_insert_at = indices[i]
# Insert the number at the specified index.
# This might require shifting existing elements.
target_array = target_array[:index_to_insert_at] + [numbers[i]] + target_array[index_to_insert_at:]
return target_array
We need to construct a new array following a specific order defined by two input arrays. The key idea is to directly insert elements into the correct positions of a new array as we iterate. To do this efficiently, we'll leverage the ability to insert at a specific location within a data structure that allows dynamic resizing and shifting of elements.
Here's how the algorithm would work step-by-step:
def create_target_array(numbers, positions):
target_array = []
# Iterate through the input arrays
for index in range(len(numbers)):
# Insert the number at the specified position
target_array.insert(positions[index], numbers[index])
return target_array
Case | How to Handle |
---|---|
Null or empty nums and index arrays | Return an empty array or throw an IllegalArgumentException if either input is null or empty. |
nums and index arrays of different lengths | Throw an IllegalArgumentException because the index array needs a corresponding value from the nums array to be inserted. |
index array contains negative indices | Throw an IllegalArgumentException since indices must be non-negative. |
index array contains indices out of bounds (>= nums.length) | Throw an IndexOutOfBoundsException as attempting to insert at such an index is invalid. |
index array contains duplicate indices | The list insertion will overwrite the previous element at this index, resolving the conflict according to the problem. |
Large input size for nums and index, potentially exceeding memory limits | Consider using a data structure like a linked list or carefully managing memory when the input size is very large to prevent out-of-memory errors. |
index array with all elements equal to 0 | The resulting array will be a reversed version of the nums array. |
nums array contains duplicate values with different corresponding indices | Each value is inserted at its specified index without interference from duplicate values. |