Array Transformation — LeetCode 1243 Python Solution

EasyLeetCode PremiumArraySimulation
Problem
#1243
Reading time
3 min

The problem

Given an initial array arr, every day you produce a new array using the array of the previous day. On the i-th day, you do the following operations on the array of day i-1 to produce the array of day i: If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.

Example

Input
arr = [6,2,3,4]
Output
[6,3,3,4]
Explanation
On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].

Python solution

Python
class Solution:
    def transformArray(self, arr: List[int]) -> List[int]:
        f = True
        while f:
            f = False
            t = arr[:]
            for i in range(1, len(t) - 1):
                if t[i] > t[i - 1] and t[i] > t[i + 1]:
                    arr[i] -= 1
                    f = True
                if t[i] < t[i - 1] and t[i] < t[i + 1]:
                    arr[i] += 1
                    f = True
        return arr

Complexity

MeasureComplexity
TimeO(n \times m)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1243. Array Transformation?
LeetCode 1243. Array Transformation is rated Easy on LeetCode.
What is the time complexity of LeetCode 1243. Array Transformation?
The Python solution on this page runs in O(n \times m).
What is the space complexity of LeetCode 1243. Array Transformation?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 1243. Array Transformation cover?
LeetCode 1243. Array Transformation is tagged Array and Simulation on LeetCode.
Is LeetCode 1243. Array Transformation a premium problem?
Yes. LeetCode 1243. Array Transformation is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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