File tree Expand file tree Collapse file tree 5 files changed +99
-0
lines changed Expand file tree Collapse file tree 5 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments