学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。
请你返回至少有多少个学生没有站在正确位置数量。该人数指的是:能让所有学生以 非递减 高度排列的必要移动人数。
示例:
输入:[1,1,4,2,1,3]
输出:3
解释:
高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。
提示:
1 <= heights.length <= 100
1 <= heights[i] <= 100
class Solution {
//先给数据排序,比较和排序后有几个不同的,即有几个站错队的
public int heightChecker(int[] heights) {
int length = heights.length;
//先将数据存一份,直接赋值不行,数组即指针
int[] temp = new int[length];
for(int i = 0; i< length; i++){
temp[i] = heights[i];
}
sort(heights);
int result = 0;
for(int i = 0; i < length; i++){
if(temp[i] != heights[i]){
result++;
}
}
return result;
}
private void sort(int[] nums){
int length = nums.length;
for(int i = 0; i< length; i++){
for(int j = i + 1; j < length; j++){
if(nums[i] > nums[j]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}
}