Skip to content

Commit cfae017

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 8977220 + f4065f9 commit cfae017

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
# Solution 1: Sort and fill
3+
# Intuition:
4+
# Ensure the below pattern (no Avg number can form at nums[i]:
5+
# nums[i-1] < nums[i] > nums[i+1]
6+
7+
# Input: nums = [6,2,0,9,7]
8+
# Sorted nums = [0,2,6,7,9]
9+
# 1st Filled arr = [0,_,2,_,6]
10+
# 2nd Filled arr = [0,7,2,9,6]
11+
12+
def rearrangeArray(self, nums: List[int]) -> List[int]:
13+
nums.sort()
14+
15+
i, j, n = 0, 0, len(nums)
16+
ans = [0]*n
17+
18+
while i < n and j < n:
19+
ans[i] = nums[j]
20+
i = i + 2
21+
j = j + 1
22+
23+
i = 1
24+
while i < n and j < n:
25+
ans[i] = nums[j]
26+
i = i + 2
27+
j = j + 1
28+
29+
return ans
30+

0 commit comments

Comments
 (0)