[
array
simulation
]
Leetcode 1243 Array Transformation
Problem statement
https://leetcode.com/problems/monthly-transactions-i/
Solution
Just do what is asked, process simulation
Complexity
Time complexity is O(nm)
, where n = len(arr)
, m = max(arr)
. Space complexity is O(n)
.
Code
class Solution:
def transformArray(self, arr):
while True:
arr2 = arr[:]
for i in range(1, len(arr) - 1):
if arr[i] < arr[i+1] and arr[i] < arr[i-1]:
arr2[i] += 1
if arr[i] > arr[i+1] and arr[i] > arr[i-1]:
arr2[i] -= 1
if arr2 == arr: return arr
arr = arr2[:]