Skip to content

Commit ed3c2e2

Browse files
committed
Time: 376 ms (5.04%), Space: 31.2 MB (63.03%) - LeetHub
1 parent 424a665 commit ed3c2e2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool canSortArray(vector<int>& nums) {
4+
5+
auto bit = [&](int x) {
6+
return __builtin_popcount(x);
7+
};
8+
auto ok = [&](int a, int b) {
9+
return (bit(a) == bit(b) && a > b);
10+
};
11+
int n = nums.size();
12+
for (int x = 0; x < (n * n); x++) {
13+
for (int i = 0; i + 1 < n; i++) {
14+
if (nums[i] > nums[i + 1]) {
15+
if (ok(nums[i], nums[i + 1])) swap(nums[i], nums[i + 1]);
16+
}
17+
}
18+
}
19+
return is_sorted(nums.begin(), nums.end());
20+
}
21+
};

0 commit comments

Comments
 (0)