Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/
- class Solution:
- def minimumOperations(self, nums: List[int]) -> int:
- from collections import Counter
- if len(nums) <= 1:
- return 0
- elif len(nums) == 2:
- if nums[1] == nums[0]:
- return 1
- else:
- return 0
- even = []
- odd = []
- for i in range(len(nums)):
- if i % 2 == 0:
- even.append(nums[i])
- else:
- odd.append(nums[i])
- def check(c1, c2):
- even_c = Counter(c1)
- odd_c = Counter(c2)
- def get_res(cur_k, cur_v):
- res = len(c1) - cur_v
- for k, v in odd_c.most_common():
- if k == cur_k:
- if len(odd_c.keys()) == 1:
- return res + v
- continue
- res += len(c2) - v
- break
- return res
- res_min = float('inf')
- even_max = even_c.most_common(1)
- val = even_max[0][1]
- for k, v in even_c.most_common():
- if v < val:
- break
- cur_res = get_res(k, v)
- if cur_res < res_min:
- res_min = cur_res
- if v == 1:
- break
- return res_min
- return min(check(even, odd), check(odd, even))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement