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. 洛谷 P4071 [SDOI2016]排列计数 题解

    P4071 [SDOI2016]排列计数 题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳 ...

  2. Luogu5206 【WC2019】数树 【容斥,生成函数】

    题目链接 第一问白给. 第二问: 设 \(b=y^{-1}\),且以下的 \(Ans\) 是除去 \(y^n\) 的. 设 \(C(T)\) 是固定了 \(T\) 中的边,再连 \(n-|T|-1\) ...

  3. QQ for Mac聊天纪录怎么查找??

    在你Mac上打开你的QQ,选择任意聊天窗口,打字的上面有6个图表快捷键,第6个就是查看聊天记录的功能键

  4. 最近公司遇到了APR攻击,顺便了解一下知识

    原因及背景 最近公司遇到了APR攻击导致整个公司研发部.测试部.客服部.工程部等几个部门统一无法上网,TV(team viewer)无法使用,部署在公网的B/S架构系统系统无法访问,开发代码上传和下载 ...

  5. 如果判断条件过多,可以直接在computed里面去返回需要判断的数据

    bad <div class="offer-item_margin" v-show="offer.supplierName || offer.supplierSto ...

  6. 【2019.11.27】SDN上机第5次作业

    参考资料: https://www.cnblogs.com/zzqsss/p/11924685.html 问答环节 描述官方教程实现了一个什么样的交换机功能? Ryu是一个基于组件的软件定义的网络框架 ...

  7. nacos启动与sql8.0的问题解决方法

    hi all! 半年多没更新,是不是以为我消失了……直接正题~ 在搭建nacos环境的时候,有这样的一项:数据库持久化配置.(官方文档),这个配置可以灵活的帮我们进行配置而不用总是重启服务. 那么问题 ...

  8. NoSql数据库Redis系列(6)——Redis数据过期策略详解

    本文对Redis的过期机制简单的讲解一下 讲解之前我们先抛出一个问题,我们知道很多时候服务器经常会用到redis作为缓存,有很多数据都是临时缓存一下,可能用过之后很久都不会再用到了(比如暂存sessi ...

  9. 时针分针角度问题c语言解法

    #include <stdio.h> //时针一小时走30度 double hour_per_hour_angle = 30.0; //先算出时针和分钟 一分钟内 分别走多少度数 //时针 ...

  10. pycharm安装pyinstaller将pygame打包成exe

    首先,使用pycharm自带的下载包工具,File-Settings-Project Interpreter,如图: 安装完成后,发现安装到了Python根目录下,我的在C:\python34\Scr ...