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. mysql 修改表名

    //重命名表 rename table table1 to table2; //重命名多个表 rename table table1 to table2,table3 to table4,table5 ...

  2. 移动端 - adb shell常用命令

    一.文件操作相关命令 //进入设备 adb shell //进入指定目录"/data/local/tmp" cd /data/local/tmp //查看目录 ls //进入根目录 ...

  3. CTF 文件上传

    目录 一.客户端校验 1.禁用JS 2.抓包改包 二.服务端校验 1.MIME类型检测 2.后缀名黑名单校验 3.后缀名白名单校验 4.内容头校验 5.竞争上传 6.过滤<?或php 两种校验方 ...

  4. MySQL服务问题

    Mysql使用命令 net start mysql net stop mysql 出现如下报错 经过查询得知可能是安装时修改过服务名称 查看服务名称的方法:这台电脑右键->管理->服务和应 ...

  5. 【转】HTTP 协议的历史演变和设计思路

    一.HTTP/0.9 HTTP 是基于 TCP/IP 协议的应用层协议.它不涉及数据包(packet)传输,主要规定了客户端和服务器之间的通信格式,默认使用80端口. 最早版本是1991年发布的0.9 ...

  6. 以SQL命令方式调用存储过程

    string str = "Data Source=.;Initial Catalog=***;Integrated Security=True"; using (SqlConne ...

  7. 利用select/poll监听多个设备详解

    如果一个应用程序去处理多个设备,例如应用程序读取网路数据,按键,串口,一般能想到的有三种方法: 方法1:串行+阻塞的方式读取:while(1) { read(标准输入);read(网络);}缺点:每当 ...

  8. postMan下使用xdebug

    增加 ?XDEBUG_SESSION_START=PHPSTORM 例: {{url}}/manage/getuserinfo?XDEBUG_SESSION_START=PHPSTORM

  9. 安装比特币区块链钱包API(Blockchain Wallet用户发送和接收比特币的简单API)

    区块链钱包API提供了一个简单的界面,商家可以用它以编程方式与钱包进行交互. 安装:要使用此API,您需要运行负责管理区块链钱包的小型本地服务. 您的应用程序通过HTTP API调用在本地与此服务进行 ...

  10. 案例:使用BeautifuSoup4的爬虫

    使用BeautifuSoup4解析器,将招聘网页上的招聘单位名称存储出来.其他信息可类似爬取即可 # -*- coding:utf-8 -*- from bs4 import BeautifulSou ...