-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path3011.cpp
30 lines (30 loc) · 909 Bytes
/
3011.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
bool canSortArray(vector<int>& nums) {
int n = nums.size();
vector<int> counts(n, 0);
for (int i = 0; i < n; ++i) {
int cnt = __builtin_popcount(nums[i]);
counts[i] = cnt;
}
int highest = -1;
int tempLowest = -1;
int tempHightest = -1;
int currCnt = -1;
for (int i = 0; i < n; ++i) {
if (counts[i] == currCnt) {
tempLowest = min(tempLowest, nums[i]);
tempHightest = max(tempHightest, nums[i]);
}
else {
if (tempLowest < highest) return false;
highest = tempHightest;
tempLowest = nums[i];
tempHightest = nums[i];
currCnt = counts[i];
}
}
if (tempLowest < highest) return false;
return true;
}
};