package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: HeightChecker
* @Author: xiaof
* @Description: 1051. 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.)
* 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.
* @Date: 2019/7/3 9:19
* @Version: 1.0
*/
public class HeightChecker { public int solution(int[] heights) { //首先排个序,然后比较一下就可以了!,来个快排吧
int[] array = new int[heights.length]; for(int i = 0; i < array.length; ++i) {
array[i] = heights[i];
} quikSort1(array, 0, array.length); //比较不同的位置
int result = 0;
for(int j = 0; j < array.length; ++j) {
if(array[j] != heights[j]) {
result++;
}
} return result;
} private void quikSort1(int array[], int start, int end) {
if(start < end) {
int mid = this.hoarePartition(array, start, end);
quikSort1(array, start, mid);
quikSort1(array, mid + 1, end);
}
} private int hoarePartition(int[] array, int start, int end) {
//对区间进行排序
int midValue = array[start];
int index1 = start, index2 = end;
//为了避免自己重复比较
do {
//左边查找比mid值大的
do {
++index1;
} while(index1 < end && array[index1] < midValue); //找到比mid小的值
do {
--index2;
} while(index2 > start && array[index2] > midValue); //交换数据
if(index1 < index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
} } while(index1 < index2); //最后我们把坑填到中间位置,因为index1和index2错开位置了,我们交换回来
// int temp = array[index1];
// array[index1] = array[index2];
// array[index2] = temp;
//填坑
array[start] = array[index2];
array[index2] = midValue; return index2;
} public static void main(String args[]) {
int A[] = {7,4,5,6,4,2,1,4,6,5,4,8,3,1,8,2,7,6,3,2};
int A1[] = {1,1,4,2,1,3};
int A2[] = {1,4,3,2};
HeightChecker fuc = new HeightChecker();
System.out.println(fuc.solution(A2));
} }

【LEETCODE】40、1051. Height Checker的更多相关文章

  1. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  2. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  6. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  7. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  8. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

  9. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

随机推荐

  1. Mongo 安装及基本操作

    一. 安装 Mongo文档: https://docs.mongodb.com/v3.6/administration/install-enterprise-linux/ Linux mongo的配置 ...

  2. MQTT 遗嘱使用

    大部分人应该有这个需求: 我想让我的APP或者上位机或者网页一登录的时候获取设备的状态   在线还是离线 设备端只需要这样设置 注意:MQTT本身有遗嘱设置 所以大家可以设置遗嘱 ,注意哈,发布的主题 ...

  3. mybatis 获取新增数据的主键

    <insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys= ...

  4. mapreduce数据处理——统计排序

    接上篇https://www.cnblogs.com/sengzhao666/p/11850849.html 2.数据处理: ·统计最受欢迎的视频/文章的Top10访问次数 (id) ·按照地市统计最 ...

  5. 【洛谷】P5024 保卫王国 (倍增)

    前言 传送门 很多人写了题解了,我就懒得写了,推荐一篇博客 那就分享一下我的理解吧(说得好像有人看一样 对于每个点都只有选与不选两种情况,所以直接用倍增预处理出来两种情况的子树之内,子树之外的最值,最 ...

  6. css文字两行或者几行显示省略号

    做这个省略的问题,突然发现显示省略号是有中英文区分的 我做两行的时候用的是以下代码,在是中文的情况下是么得问题,到了英文下发现不起作用了 width: 250px; overflow: hidden; ...

  7. java.lang.Thread类的静态方法sleep()和yield()的比较

    [线程让步yield()方法] yield()方法可以让当前正在执行的线程暂停,但它不会阻塞该线程,它只是将该线程从运行状态转入就绪状态. 只是让当前的线程暂停一下,让系统的线程调度器重新调度一次. ...

  8. Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本

    本篇文章主要介绍了Python 通过selenium实现毫秒级自动抢购的示例代码,通过扫码登录即可自动完成一系列操作,抢购时间精确至毫秒,可抢加购物车等待时间结算的,也可以抢聚划算的商品. 该思路可运 ...

  9. The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.

    https://www.opentechguides.com/how-to/article/powershell/105/powershel-security-error.html Unblockin ...

  10. /etc/bashrc

    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[3 ...