-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeight_Checker.java
More file actions
34 lines (31 loc) · 882 Bytes
/
Height_Checker.java
File metadata and controls
34 lines (31 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.leet_code;
import java.util.Arrays;
public class Height_Checker {
public static void main(String[] args) {
int[] arr={1,1,4,2,1,3};
System.out.println(heightChecker(arr));
}
static int heightChecker(int[] heights){
int[] arr = heights.clone();
boolean swap ;
int count=0;
for (int i = 0; i < arr.length; i++) {
swap=false;
for (int j = 1; j < arr.length-i; j++) {
if (arr[j-1] >arr[j]) {
int temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
swap=true;
}
}
if (!swap){break;}
}
for (int i = 0; i < arr.length ; i++) {
if (arr[i] != heights[i]) {
count+=1;
}
}
return count;
}
}