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. vue 自动px单位自动转换rem

    vue 适配移动端 假设设计图是375 第一步 安装 lib-flexible npm i lib-flexible --save 第二步 安装 px2rem-loader npm install p ...

  2. 【数位DP】数字统计

    题目 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 数位DP (1)分情况,逐位讨论. (2)模型:计算在[L,R]中有多少个数满足条件. (3)套路:将 ...

  3. 洛谷 P4568 [JLOI2011]飞行路线 题解

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...

  4. QBXT 2017GoKing problems 补完计划

    10.11 Updata : 烦死了...麻烦死了...不补了..就这些吧 20171001 上: 100 + 90 + 90 = 280 = rank 8 T1 /* T1 从最大的数开始倒着枚举 ...

  5. mysql find_in_set 函数 使用方法

    mysql> select * from user; +------+----------+-----------+ | id | name | address | +------+------ ...

  6. #C++初学记录(动态规划 被3整除的子序列)

    原题:牛客网 动态规划dynamic programming 的入门级题目 题目描述 : 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e9+7取模 输入描述: 输入一 ...

  7. Unity3D Substance designer Sub 欧洲小镇场景制作视频教程 中文字幕

    大小6.53G,中文字幕 扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  8. ES 基本用法

    转自:https://www.cnblogs.com/rodge-run/p/7760308.html ES的基本概念 1> 集群和节点 一个es集群是由一个或多和es节点组成的集合 每一个集群 ...

  9. Python实例100个(基于最新Python3.7版本)

    Python3 100例 原题地址:   http://www.runoob.com/python/python-100-examples.html    git地址:    https://gith ...

  10. Maven构建报错问题解决

    [ERROR] Failed to execute goal on project zepeto-admin: Could not resolve dependencies -SNAPSHOT: Fa ...