【HackerRank】Find the Median(Partition找到数组中位数)
In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an array?
Challenge
Given a list of numbers, can you find the median?
Input Format
There will be two lines of input:
- n - the size of the array
- ar - n numbers that makes up the array
Output Format
Output one integer, the median.
Constraints
1<= n <= 1000001
-10000 <= x <= 10000 , x ∈ ar
There will be an odd number of elements.
题解:久闻Partition函数可以用来找到乱序数组的中位数,今天终于实现了一把。
设置一个变量need为数组长度的一半,另一个变量hasFound为当前比找到的比中位数小的数的个数,当hasFound=need的时候,我们就找到了中位数。
在每次Partition后,看比pivot小的那部分数组有多少个元素,如果hasFound加上这部分元素正好等于need,那么pivot就是所求的中位数。如果hasFound加上这部分元素大于need,说明比pivot小的这部分数组需要继续划分,递归的调用Partition;如果hasFound加上这部分元素小于need,那么就把这部分元素个数加到hasFound上,并且继续划分比pivot大的那部分数组。
例如数组:3 1 4 5 2,找中位数的过程如下图所示:

代码如下:
import java.util.*;
public class Solution {
private static int need = 0;
private static int hasFound = 0;
private static void swap(int[] ar,int i,int j){
int temp = ar[i];
ar[i]= ar[j];
ar[j]=temp;
}
private static int Partition(int[] ar,int start,int end){
int pivot = ar[end];
int i = start;
int j = start;
while(i < end){
if(ar[i] >= pivot)
i++;
else if(ar[i] < pivot){
swap(ar,i,j);
i++;
j++;
}
}
swap(ar, j, end);
if(hasFound+j-start+1==need)
return pivot;
else if(hasFound+j-start+1<need){
hasFound += j-start+1;
return Partition(ar, j+1, end);
}
else {
return Partition(ar, start, j-1);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
need = n%2==0?n/2:n/2+1;
int[] ar = new int[n];
for(int i = 0;i < n;i ++)
ar[i] = in.nextInt();
System.out.println(Partition(ar, 0, n-1));
}
}
以上代码就很容易扩展到找寻数组中第k小的数了,只要改变need变量的值即可。
【HackerRank】Find the Median(Partition找到数组中位数)的更多相关文章
- 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?
如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...
- luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)
链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...
- 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- POJ 3579:Median 差值的中位数
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4680 Accepted: 1452 Descriptio ...
- Java 找到数组中两个元素相加等于指定数的所有组合
思路1:可以用hash表来存储数组中的元素,这样我们取得一个数后,去判断sum - val 在不在数组中,如果在数组中,则找到了一对二元组,它们的和为sum,该算法的缺点就是需要用到一个hash表,增 ...
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- leetcode-1 Two Sum 找到数组中两数字和为指定和
问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...
- URAL 1306 - Sequence Median 小内存求中位数
[题意]给出n(1~250000)个数(int以内),求中位数 [题解]一开始直接sort,发现MLE,才发现内存限制1024k,那么就不能开int[250000]的数组了(4*250000=1,00 ...
- 求两个排序数组中位数 C++
题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nu ...
随机推荐
- jq和thinkphp经常使用的几种ajax
第一种方法 第二种方法 jquery方法: MessageAction.class.php <?php class MessageAction extends Action{ functio ...
- java.lang.IllegalArgumentException: column '_id' does not exist问题的解决方案
我在使用SimpleCursorAdapter的过程中遇到了问题: java.lang.IllegalArgumentException: column '_id' does not exist 这个 ...
- 如何居中div?
如何居中div? 水平居中 1 //给div设置一个宽度,然后添加margin:0 auto属性 2 3 div{ 4 width:200px; 5 margin:0 auto; 6 } 让绝对定位的 ...
- 用ActivatedRoute获取url中的参数
突然让我用ActivatedRoute import {Injectable} from "@angular/core"; import {ActivatedRoute} from ...
- Mustache(2)
Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档.当然不同的开发语言有不同模板引擎,如 Javascript 下的 Hogan . ...
- 如何顺利解决mac下命令不管用的情况
背景: 昨晚通过brew安装了node,结果导致我的终端除了cd和ls管用外,其他的命令都不管用了,网上搜索了一大堆,结果没有一个能正确解决我的问题的,记录一下吧. 打开终端就显示: -bash: t ...
- 4、easyUI-七种布局(layout)
1.为网页创建边框布局 边框布局(border layout)提供五个区域:east.west.north.south.center.以下是一些通常用法: north 区域可以用来显示网站的标语. s ...
- poj2420(模拟退火大法好)
// // main.cpp // poj2420 // // Created by 陈加寿 on 16/2/13. // Copyright © 2016年 chenhuan001. All rig ...
- java 多种判断key是否在map中存在的方法
java 中有时候会遇到判断传过来的map里是否包含了指定的key,我目前只发现两种办法,如果有其他方法欢迎补充 我添加上去: HashMap map = new HashMap(); map.put ...
- 在Visual Studio 2015的Cordova项目中使用Gulp
之前一直是在vs 2013中使用Cordova来开发移动app(目前有iPad版/iPhone版/安卓版),准备到下一个milestone的时候升级到2015,这两天在尝试各种东西. 2015中的co ...