Skip to content

Commit 424170b

Browse files
authored
Create Leetcode_1051.cpp
1 parent 081a30d commit 424170b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Leetcode_1051.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
// 1051. Height Checker
5+
class Solution {
6+
public:
7+
int heightChecker(vector<int>& heights) {
8+
int res = 0;
9+
int size = heights.size();
10+
vector<int> exp;
11+
for(int val : heights) {
12+
exp.push_back(val);
13+
}
14+
for(int i=0;i<size;i++) {
15+
for(int j=0;j<size-1;j++){
16+
if(exp[j] > exp[j+1]){
17+
swap(exp[j],exp[j+1]);
18+
}
19+
}
20+
}
21+
for(int i=0;i<exp.size();i++){
22+
if(exp[i] != heights[i]){
23+
res++;
24+
}
25+
}
26+
return res;
27+
}
28+
};
29+
int main() {
30+
Solution s;
31+
vector<int> heights = {1,1,4,2,1,3};
32+
cout << s.heightChecker(heights) << endl; // Output: 3
33+
return 0;
34+
}

0 commit comments

Comments
 (0)