Skip to content

Latest commit

 

History

History
146 lines (112 loc) · 3.27 KB

File metadata and controls

146 lines (112 loc) · 3.27 KB

English Version

题目描述

学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。

请你返回能让所有学生以 非递减 高度排列的最小必要移动人数。

注意,当一组学生被选中时,他们之间可以以任何可能的方式重新排序,而未被选中的学生应该保持不动。

 

示例:

输入:heights = [1,1,4,2,1,3]
输出:3 
解释:
当前数组:[1,1,4,2,1,3]
目标数组:[1,1,1,2,3,4]
在下标 2 处(从 0 开始计数)出现 4 vs 1 ,所以我们必须移动这名学生。
在下标 4 处(从 0 开始计数)出现 1 vs 3 ,所以我们必须移动这名学生。
在下标 5 处(从 0 开始计数)出现 3 vs 4 ,所以我们必须移动这名学生。

示例 2:

输入:heights = [5,1,2,3,4]
输出:5

示例 3:

输入:heights = [1,2,3,4,5]
输出:0

 

提示:

  • 1 <= heights.length <= 100
  • 1 <= heights[i] <= 100

解法

Python3

class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        bucket = [0] * 101
        for h in heights:
            bucket[h] += 1
        res = i = 0
        for j in range(1, 101):
            while bucket[j] > 0:
                bucket[j] -= 1
                if heights[i] != j:
                    res += 1
                i += 1
        return res
class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        expected = sorted(heights)
        return sum(1 for i, h in enumerate(heights) if h != expected[i])

Java

class Solution {
    public int heightChecker(int[] heights) {
        int[] expected = Arrays.copyOf(heights, heights.length);
        Arrays.sort(expected);
        int res = 0;
        for (int i = 0; i < heights.length; ++i) {
            if (heights[i] != expected[i]) {
                ++res;
            }
        }
        return res;
    }
}

C++

class Solution {
public:
    int heightChecker(vector<int>& heights) {
        vector<int> expected = heights;
        sort(expected.begin(), expected.end());
        int res = 0;
        for (int i = 0; i < heights.size(); ++i)
        {
            if (heights[i] != expected[i]) ++res;
        }
        return res;
    }
};

Go

func heightChecker(heights []int) int {
    expected := make([]int, len(heights))
    copy(expected, heights)
    sort.Ints(expected)
    res := 0
    for i, h := range heights {
        if h != expected[i] {
            res++
        }
    }
    return res
}

...