作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/height-checker/

题目描述

Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students not standing in the right positions. (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)

Example 1:

Input: [1,1,4,2,1,3]
Output: 3
Explanation:
Students with heights 4, 3 and the last 1 are not standing in the right positions.

Note:

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

题目大意

一组数字和排序后的这组数字有多少个位置是不一致的?

解题方法

排序比较

这个题这么直白啊,直接问和排序后的数字有多少个位置是不一样的。所以排序之后比较就行了呀。看了下数字的范围,竟然只有100个!哪怕是100000直接排序比较也应该会通过!

时间复杂度是O(NlogN)。

Python代码如下:

class Solution(object):
def heightChecker(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
return sum(a != b for a, b in zip(sorted(heights), heights))

C++代码如下:

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

日期

2019 年 6 月 8 日 —— 刷题尽量不要停

【LeetCode】1051. Height Checker 解题报告(Python & C++)的更多相关文章

  1. leetcode 1051. Height Checker

    Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minim ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 【LEETCODE】40、1051. Height Checker

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【Leetcode_easy】1051. Height Checker

    problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>&a ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. python10-高阶函数

    def use_filer(l): """ 获取指定列表/元组中的奇数 :param l: lsit/tuple :return: """ ...

  2. Mysql-单个left join 计算逻辑(一对多问题)

    BUG背景: 我们有一个订单表 和 一个 物流表 它们通过 订单ID 进行一对一的关系绑定.但是由于物流表在保存订单信息的时候没有做判断该订单是否已经有物流信息,这就变成同一个订单id在物流表中存在多 ...

  3. 用jquery的prop方法操作checkbox

    prop设置checkbox选中 $('#checkbox-id').prop("checked",true) 判断checkbox是否选中,if ($('#checkbox-id ...

  4. 2 — springboot的原理

    1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...

  5. 日常Java 2021/11/3

    java网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来.java.net包中J2SE的APl包含有类和接口,它们提供低层次的通信细节.你可以直接使用这些类和接口, ...

  6. pop回指定控制器

    //OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...

  7. Java Web 实现Mysql 数据库备份与还原

    前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...

  8. JAVA平台AOP技术研究

    3.1 Java平台AOP技术概览 3.1.1 AOP技术在Java平台中的应用 AOP在实验室应用和商业应用上,Java平台始终走在前面.从最初也是目前最成熟的AOP工具--AspectJ,到目前已 ...

  9. my39_InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析

    MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock): 锁定索引记录间隙,确保索引记录的间隙不变.间隙锁是 ...

  10. 漏洞扫描器-AWVS

    目录 介绍 漏洞扫描 网络爬虫==漏洞分析.验证 主机发现 子域名探测 SQL注入 HTTP头编辑 HTTP监听 介绍 AWVS为Acunetix Web Vulnarability Scanner的 ...