EasyNo. 1243LeetCode Premium
Array Transformation — LeetCode 1243 Python Solution
- Topics
- Array, Simulation
- 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
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
- Time
- O(n × m)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0495 | Teemo Attacking | Easy |
| 0985 | Sum of Even Numbers After Queries | Medium |
| 1389 | Create Target Array in the Given Order | Easy |
| 1409 | Queries on a Permutation With Key | Medium |
| 1503 | Last Moment Before All Ants Fall Out of a Plank | Medium |
| 1535 | Find the Winner of an Array Game | Medium |
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 × 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.