2170. Minimum Operations to Make the Array Alternating Medium

@problem@discussion
#Array#Hash Table#Greedy#Counting



1/**
2 * [2170] Minimum Operations to Make the Array Alternating
3 *
4 * You are given a 0-indexed array nums consisting of n positive integers.
5 * The array nums is called alternating if:
6 * 
7 * 	nums[i - 2] == nums[i], where 2 <= i <= n - 1.
8 * 	nums[i - 1] != nums[i], where 1 <= i <= n - 1.
9 * 
10 * In one operation, you can choose an index i and change nums[i] into any positive integer.
11 * Return the minimum number of operations required to make the array alternating.
12 *  
13 * Example 1:
14 * 
15 * Input: nums = [3,1,3,2,4,3]
16 * Output: 3
17 * Explanation:
18 * One way to make the array alternating is by converting it to [3,1,3,<u>1</u>,<u>3</u>,<u>1</u>].
19 * The number of operations required in this case is 3.
20 * It can be proven that it is not possible to make the array alternating in less than 3 operations. 
21 * 
22 * Example 2:
23 * 
24 * Input: nums = [1,2,2,2,2]
25 * Output: 2
26 * Explanation:
27 * One way to make the array alternating is by converting it to [1,2,<u>1</u>,2,<u>1</u>].
28 * The number of operations required in this case is 2.
29 * Note that the array cannot be converted to [<u>2</u>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= nums.length <= 10^5
35 * 	1 <= nums[i] <= 10^5
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/
41// discuss: https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn minimum_operations(nums: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2170() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.