Leetcode #1389: Create Target Array in the Given Order
In this guide, we solve Leetcode #1389 Create Target Array in the Given Order in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty.
Quick Facts
- Difficulty: Easy
- Premium: No
- Tags: Array, Simulation
Intuition
The rules are explicit, so simulating the process step by step is safest.
Careful state updates prevent subtle bugs.
Approach
Translate the rules into state updates and apply them in order.
Track the final state or aggregate as required.
Steps:
- Translate rules into state updates.
- Iterate for each step.
- Return the final state.
Example
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]
Python Solution
class Solution:
def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
target = []
for x, i in zip(nums, index):
target.insert(i, x)
return target
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.