Array Transformation — LeetCode 1243 Python Solution
EasyLeetCode PremiumArraySimulation
- Problem
- #1243
- Reading time
- 3 min
- Source
- leetcode.com
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 arrComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times m) |
| Space | O(n) auxiliary |
Related problems
LeetCode 495Teemo AttackingEasyLeetCode 985Sum of Even Numbers After QueriesMediumLeetCode 1389Create Target Array in the Given OrderEasyLeetCode 1409Queries on a Permutation With KeyMediumLeetCode 1503Last Moment Before All Ants Fall Out of a PlankMediumLeetCode 1535Find the Winner of an Array GameMedium
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.