Skip to content

Commit beea75a

Browse files
committed
2170. Minimum Operations to Make the Array Alternating: RETRY
1 parent d5962bb commit beea75a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,3 +1640,4 @@ mod s2165_smallest_value_of_the_rearranged_number;
16401640
mod s2166_design_bitset;
16411641
mod s2167_minimum_time_to_remove_all_cars_containing_illegal_goods;
16421642
mod s2169_count_operations_to_obtain_zero;
1643+
mod s2170_minimum_operations_to_make_the_array_alternating;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
*/
38+
pub 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+
45+
impl Solution {
46+
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
47+
0
48+
}
49+
}
50+
51+
// submission codes end
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
#[ignore]
59+
fn test_2170_example_1() {
60+
let nums = vec![3, 1, 3, 2, 4, 3];
61+
62+
let result = 3;
63+
64+
assert_eq!(Solution::minimum_operations(nums), result);
65+
}
66+
67+
#[test]
68+
#[ignore]
69+
fn test_2170_example_2() {
70+
let nums = vec![1, 2, 2, 2, 2];
71+
72+
let result = 2;
73+
74+
assert_eq!(Solution::minimum_operations(nums), result);
75+
}
76+
}

0 commit comments

Comments
 (0)