leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2134.cpp (517B)


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