3011.cpp (671B)
1 class Solution { 2 public: 3 bool canSortArray(vector<int> &nums) { 4 const int n = size(nums); 5 6 int i = 0, prev = 0, cnt = std::popcount((unsigned)nums[0]); 7 int mini = nums[0], maxi = nums[0]; 8 for (int j = 1; j < n; j++) { 9 const int crnt = std::popcount((unsigned)nums[j]); 10 if (cnt == crnt) { 11 mini = min(mini, nums[j]); 12 maxi = max(maxi, nums[j]); 13 continue; 14 } 15 if (mini < prev) return false; 16 prev = maxi; 17 mini = maxi = nums[j]; 18 cnt = crnt; 19 i = j; 20 } 21 22 return mini >= prev; 23 } 24 };