You are given a 0-indexed array nums
consisting of n
positive integers.
The array nums
is called alternating if:
nums[i - 2] == nums[i]
, where 2 <= i <= n - 1
.nums[i - 1] != nums[i]
, where 1 <= i <= n - 1
.In one operation, you can choose an index i
and change nums[i]
into any positive integer.
Return the minimum number of operations required to make the array alternating.
For example:
nums = [3,1,3,2,4,3]
should return 3
because one way to make the array alternating is by converting it to [3,1,3,1,3,1]
. The number of operations required in this case is 3.
nums = [1,2,2,2,2]
should return 2
because one way to make the array alternating is by converting it to [1,2,1,2,1]
. The number of operations required in this case is 2.
Let's start by outlining a brute-force approach to solve this problem, followed by a more optimized solution.
def min_operations_brute_force(nums):
n = len(nums)
if n <= 1:
return 0
even_counts = {}
odd_counts = {}
for i in range(0, n, 2):
even_counts[nums[i]] = even_counts.get(nums[i], 0) + 1
for i in range(1, n, 2):
odd_counts[nums[i]] = odd_counts.get(nums[i], 0) + 1
even_nums = list(even_counts.keys())
odd_nums = list(odd_counts.keys())
min_ops = float('inf')
for even_num in even_nums:
for odd_num in odd_nums:
if even_num != odd_num:
ops = 0
for i in range(0, n, 2):
if nums[i] != even_num:
ops += 1
for i in range(1, n, 2):
if nums[i] != odd_num:
ops += 1
min_ops = min(min_ops, ops)
return min_ops
n - count_even_most - count_odd_most
.from collections import Counter
def min_operations(nums):
n = len(nums)
even_nums = nums[::2]
odd_nums = nums[1::2]
even_counts = Counter(even_nums)
odd_counts = Counter(odd_nums)
even_most_common = even_counts.most_common(2)
odd_most_common = odd_counts.most_common(2)
if even_most_common[0][0] != odd_most_common[0][0]:
return n - even_most_common[0][1] - odd_most_common[0][1]
else:
if len(even_most_common) == 1 and len(odd_most_common) == 1:
return min(len(even_nums), len(odd_nums))
elif len(even_most_common) == 1:
return n - even_most_common[0][1] - odd_most_common[1][1]
elif len(odd_most_common) == 1:
return n - even_most_common[1][1] - odd_most_common[0][1]
else:
return min(n - even_most_common[0][1] - odd_most_common[1][1],
n - even_most_common[1][1] - odd_most_common[0][1])
nums = [3, 1, 3, 2, 4, 3]
print(min_operations(nums))
nums = [1, 2, 2, 2, 2]
print(min_operations(nums))
The optimized approach uses Counter
to count occurrences, which takes O(n) time. Finding the most common elements takes O(1) because we are only retrieving the top 2. Therefore, the overall time complexity is O(n).
The space complexity is O(n) because, in the worst case, the Counter
objects even_counts
and odd_counts
can store up to n/2
distinct elements each.