Create Target Array in the Given Order — LeetCode 1389 Python Solution

EasyArraySimulation
Problem
#1389
Reading time
2 min

The problem

Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty.

Example

Input
nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output
[0,4,1,3,2]
Explanation
nums index target

Python solution

Python
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

MeasureComplexity
TimeO(n^2)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1389. Create Target Array in the Given Order?
LeetCode 1389. Create Target Array in the Given Order is rated Easy on LeetCode.
What is the time complexity of LeetCode 1389. Create Target Array in the Given Order?
The Python solution on this page runs in O(n^2).
What is the space complexity of LeetCode 1389. Create Target Array in the Given Order?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 1389. Create Target Array in the Given Order cover?
LeetCode 1389. Create Target Array in the Given Order is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview