2134.cpp (517B)
1 class Solution { 2 public: 3 int minSwaps(const vector<int> &nums) const { 4 const int total = accumulate(begin(nums), end(nums), 0); 5 const int n = size(nums); 6 7 if (total == n) return 0; 8 9 int cnt = 0, res = INT_MAX; 10 for (int i = 0; i < total; i++) 11 cnt += nums[i]; 12 for (int j = 0, k = total; j < n; j++) { 13 res = min(res, total - cnt); 14 cnt += nums[k] - nums[j]; 15 if (++k == n) k = 0; 16 } 17 18 return res; 19 } 20 };