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. snmp-get

    使用mibbroser可以连接到监控主机,可以获取主机mib信息 使用walk出的oid就可以获取到对应的值, 使用 -O fn 可以将返回的字符创形式的键改为数字型oid oid还有一种字符串的形式 ...

  2. A. Vova and Train ( Codeforces Round #515 (Div. 3) )

    题意:一条 L 长的路,一列车长在这条路的 l 到 r 之间,只有在 v 倍数时有灯,但是在 l 到 r 之间的灯是看不见的,问最大看见的灯的个数? 题解:L / v 表示总共的灯的个数, r / v ...

  3. zookeeper、hbase集成kerberos

    1.KDC创建principal 1.1.创建认证用户 登陆到kdc服务器,使用root或者可以使用root权限的普通用户操作: # kadmin.local -q “addprinc -randke ...

  4. win7企业版激活

    win7企业版激活:(亲测有效) 打开命令提示符,依次执行下面的代码,分别表示:安装win7企业版密钥,设置kms服务器,激活win7企业版,查询激活期限,kms一般是180天,到期后再次激活. sl ...

  5. Tkinter 之文件管理器

    一.效果图 二.功能描述 1.打开文件菜单中的打开按钮,可以选择目录. 2.可以查看各种类型的图片. 3.可以编辑文本. 4.显示行号功能,可改变目录显示的宽度. 三.使用的标签 1.Menu 2.F ...

  6. windows 共享文件夹

    windows 共享文件夹 同步工作组 右键单击"计算机",选择"属性" 更改设置 单击"更改". 输入工作组 和 主机名 启计算机使更改生 ...

  7. FusionInsight,一个融合的大数据平台

    随着物联网技术和应用的普及,以运营商.互联网以及实体经济行业为代表的企业产生了越来越多的数据,大数据的发展越来越蓬勃. 从2007年开始,大数据应用成为很多企业的需求,2012年兴起并产生了大数据平台 ...

  8. SQLServer 截取函数 substring函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...

  9. JMeter压测“java.net.SocketException: Socket closed”解决方法 - Andrea-Pirlo

    报错详情: 引起 java.net.SocketException: Socket closed 错误的原因通常是 未设置连接的超时时间. 解决方法: 该问题可以尝试通过以下方法解决. 如果在 HTT ...

  10. 【linux】CentOS: Sudo: unable to initialize policy plugin

    背景: 在centos7 下 使用sudo 命令对www用户生成ssh秘钥 .报错 解决办法: yum remove sudo yum install sudo 在执行就ok了