Minimum Moves to Make Array Complementary LeetCode solution in Python

1
class Solution:
2
def minMoves(self, nums: List[int], limit: int) -> int:
3
n = len(nums)
4
overlay_arr = [0] * (2 * limit + 2)
5
for i in range(n // 2):
6
left_boundary = min(nums[i], nums[n - 1 - i]) + 1
7
no_move_value = nums[i] + nums[n - 1 - i]
8
right_boundary = max(nums[i], nums[n - 1 - i]) + limit
9
overlay_arr[left_boundary] -= 1
10
overlay_arr[no_move_value] -= 1
11
overlay_arr[no_move_value + 1] += 1
12
overlay_arr[right_boundary + 1] += 1
13
curr_moves = n # initial assumption of two moves for each pair
14
res = float("inf")
15
# start Sweeping
16
for i in range(2, 2 * limit + 1):
17
curr_moves += overlay_arr[i]
18
res = min(res, curr_moves)
19
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0