Skip to content

3353. Minimum Total Operations 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int minOperations(vector<int>& nums) {
    int ans = 0;
    for (int i = 1; i < nums.size(); ++i)
      if (nums[i] != nums[i - 1])
        ++ans;
    return ans;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int minOperations(int[] nums) {
    int ans = 0;
    for (int i = 1; i < nums.length; ++i)
      if (nums[i] != nums[i - 1])
        ++ans;
    return ans;
  }
}
1
2
3
class Solution:
  def minOperations(self, nums: list[int]) -> int:
    return sum(a != b for a, b in itertools.pairwise(nums))