【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 ...
随机推荐
- 12:Web及MySQL服务异常监测案例
[root@db01 scripts]# cat db_check.sh #!/bin/bash db_num=$(mysql -h172. -P3306 -uroot -poldboy123 -e ...
- Linux make语法
make是一种控制编译或者重复编译软件的工具. make可以自动关键软件的编译内容.方式和时机,从而使程序员把更多的精力集中在编写代码上. make主要的机制是在命令行键入make命令,make会自动 ...
- PHP中通过数组遍历找出最小值
举例: $a = array(1,2,4,0,9,8,6);//定义一个数组 $len = count($a);//获取数组的长度 $min = $a[0];//默认情况下数组的第一个值是最小的 fo ...
- Laravel中创建控制器
<?php /** * Created by PhpStorm. * User: chuang * Date: 17-1-14 * Time: 下午4:29 */ namespace App\H ...
- PDO drivers no value in Windows
学习php编程遇到 Uncaught exception 'PDOException' with message 'could not find driver' 或者 Undefined class ...
- JDK之ThreadLocal分析
ThreadLocal是在是Thread的一个局部变量,今天我来分析了一下这个类 先看ThreadLocal的set方法 public void set(T value) { Thread t = T ...
- linux 下shell程序(二)
输入和输出 输入指的是Shell程序读入数据.有从文件读取.从用户输入读取等方式读入数据.输出指的是Shell程序的运行 结果的处理,可以显示到屏幕或保存到文件. 用ceho命令输出结果 echo $ ...
- python学习【第七篇】python文件操作
一.文件操作过程 1. 打开文件,得到文件句柄并赋值给一个变量2. 通过句柄对文件进行操作3. 关闭文件 # 1.打开文件,得到文件句柄 f_handle = open('aa.txt', 'r', ...
- 获取字符串已utf-8表示的字节数
private static int utf8Length(String string) { /** Returns the number of bytes required to write thi ...
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 4)
(四)列表附件 列表的附件也是文件系统的一部分,它依附于普通列表的列表条目之上(文档库没有附件),它的操作在一些地方和文档库中文档的操作非常类似. 1.附件的读取 一个列表条目的附件可以使用SPL ...