Skip to content

Commit 82a7474

Browse files
authored
Create height-checker.cpp
1 parent 4cb038b commit 82a7474

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/height-checker.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int heightChecker(vector<int>& heights) {
7+
vector<int> sorted_heights{heights};
8+
sort(sorted_heights.begin(), sorted_heights.end());
9+
int result = 0;
10+
for (int i = 0; i < heights.size(); ++i) {
11+
result += static_cast<int>(heights[i] != sorted_heights[i]);
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)