Skip to content

Commit 5574803

Browse files
committed
add problem #1051 solutions
Add problem #1051 solutions.
1 parent 0645fc8 commit 5574803

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

1051/main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int cmp(const void *a, const void *b) {
2+
int va = *(const int *)a;
3+
int vb = *(const int *)b;
4+
return (va > vb) - (va < vb);
5+
}
6+
7+
int heightChecker(int* heights, int heightsSize){
8+
int n = heightsSize;
9+
int rightPosition[n];
10+
for(int i = 0; i < n; i++) {
11+
rightPosition[i] = heights[i];
12+
}
13+
14+
qsort(rightPosition, n, sizeof(int), cmp);
15+
16+
int result = 0;
17+
for(int i = 0; i < n; i++) {
18+
if(rightPosition[i] != heights[i]) {
19+
result++;
20+
}
21+
}
22+
23+
return result;
24+
}

1051/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int heightChecker(vector<int>& heights) {
4+
int n = heights.size();
5+
int rightPosition[n];
6+
7+
for(int i = 0; i < n; i++) {
8+
rightPosition[i] = heights[i];
9+
}
10+
11+
sort(rightPosition, rightPosition + n);
12+
13+
int result = 0;
14+
for(int i = 0; i < n; i++) {
15+
if(heights[i] != rightPosition[i]) {
16+
result++;
17+
}
18+
}
19+
20+
return result;
21+
}
22+
};

1051/main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int heightChecker(int[] heights) {
3+
int n = heights.length;
4+
int[] rightPosition = new int[n];
5+
for(int i = 0; i < n; i++) {
6+
rightPosition[i] = heights[i];
7+
}
8+
9+
Arrays.sort(rightPosition);
10+
11+
int result = 0;
12+
for(int i = 0; i < n; i++) {
13+
if(rightPosition[i] != heights[i]) {
14+
result++;
15+
}
16+
}
17+
18+
return result;
19+
}
20+
}

1051/main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def heightChecker(self, heights: List[int]) -> int:
3+
n = len(heights)
4+
rightPosition = [0] * n
5+
for i in range(n):
6+
rightPosition[i] = heights[i]
7+
8+
rightPosition.sort()
9+
10+
result = 0
11+
for i in range(n):
12+
if rightPosition[i] != heights[i]:
13+
result += 1
14+
15+
return result

1051/main.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# @param {Integer[]} heights
2+
# @return {Integer}
3+
def height_checker(heights)
4+
n = heights.size
5+
rightPosition = Array.new(n)
6+
n.times do |i|
7+
rightPosition[i] = heights[i]
8+
end
9+
10+
rightPosition.sort!
11+
12+
result = 0
13+
n.times do |i|
14+
result += 1 if rightPosition[i] != heights[i]
15+
end
16+
17+
result
18+
end

0 commit comments

Comments
 (0)