Array With Elements Not Equal to Average of Neighbors LeetCode solution in Python

1
class Solution:
2
def rearrangeArray(self, nums: List[int]) -> List[int]:
3
for i in range(1, len(nums) - 1):
4
pre = nums[i - 1]
5
current = nums[i]
6
next = nums[i + 1]
7

8
# If block will run when we meet 1 2 3 or 6 4 2
9
if (pre < current < next) or (pre > current > next):
10
# Swap next and current
11
# For example:
12
# 1 2 3 -> 1 3 2
13
# 6 4 2 -> 6 2 4
14
nums[i + 1], nums[i] = nums[i], nums[i + 1]
15

16
return nums

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0