LeetCode每日一题: 高度检查器(No.1051)

栏目: 编程工具 · 发布时间: 6年前

学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。
请你返回至少有多少个学生没有站在正确位置数量。
该人数指的是:能让所有学生以 非递减 高度排列的必要移动人数。
复制代码

示例:

输入:[1,1,4,2,1,3]
输出:3
解释:
高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。
复制代码

思考:

先将数组排序,再与原数组中元素比较,对应位置元素不相同学生人数+1。
复制代码

实现:

class Solution {
    public int heightChecker(int[] heights) {
        int[] copy = Arrays.copyOf(heights, heights.length);
        Arrays.sort(copy);
        int diff = 0;
        for (int count = 0; count < heights.length; count++) {
            if (heights[count] != copy[count]) {
                diff++;
            }
        }
        return diff;
    }
}复制代码

以上所述就是小编给大家介绍的《LeetCode每日一题: 高度检查器(No.1051)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Out of Control

Out of Control

Kevin Kelly / Basic Books / 1995-4-14 / USD 22.95

Out of Control is a summary of what we know about self-sustaining systems, both living ones such as a tropical wetland, or an artificial one, such as a computer simulation of our planet. The last chap......一起来看看 《Out of Control》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试