其他分享
首页 > 其他分享> > 1051. 高度检查器

1051. 高度检查器

作者:互联网

学校打算为全体学生拍一张年度纪念照。根据要求,学生需要按照 非递减 的高度顺序排成一行。

排序后的高度情况用整数数组 expected 表示,其中 expected[i] 是预计排在这一行中第 i 位的学生的高度(下标从 0 开始)。

给你一个整数数组 heights ,表示 当前学生站位 的高度情况。heights[i] 是这一行中第 i 位学生的高度(下标从 0 开始)。

返回满足 heights[i] != expected[i] 的 下标数量 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/height-checker
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public int heightChecker(int[] heights) {
        int[] cnt = new int[101];
        for (int height : heights) {
            cnt[height]++;
        }
        int ret = 0;
        int index = 0;
        for (int i = 0; i < cnt.length; ++i) {
            while (cnt[i]-- > 0) {
                if (heights[index++] != i) {
                    ret++;
                }
            }
        }
        return ret;
    }
}

标签:1051,cnt,检查,int,高度,heights,++,height
来源: https://www.cnblogs.com/tianyiya/p/15698348.html