【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 ...
随机推荐
- Android实现录屏直播(二)需求才是硬道理之产品功能调研
请尊重分享成果,转载请注明出处,本文来自Coder包子哥,原文链接:http://blog.csdn.net/zxccxzzxz/article/details/54254244 前面的Android ...
- SlidingMenu官方实例分析4——AttachExample
AttachExample这个类没有继承BaseActivity,而是FragmentActivity,写到这好像感悟到了 为什么官方现在都推荐使用Fragment而不是Activity,因为Frag ...
- SQL2008删除大量数据
常见问题:工作中数据库难免产生大量的日志,而用户可能关心的只有最近一个月左右的,这些日志占用了服务器磁盘,还可能影响了服务运行效率.甚至在数据库迁移时更因为体积而带来巨大麻烦. 那么,在需要时,删除不 ...
- day3笔记
一.内容回顾 1.break:停止当前循环,后面的程序不会运行,跳出循环. 跳出while循环:1,改变条件.2.break continue:结束本次循环,继续下一次循环. 2.格式化输出:%%可以 ...
- ubuntu虚拟化平台libvrit-bin
http://download.cirros-cloud.net/0.3.5/ 下载 cirros-0.3.5-x86_64-disk.img 因为 KVM(准确说是 Libvirt)默认不接受远程管 ...
- ubuntu环境初始化
0. 在Ubuntu系统中永久修改主机名也比较简单.主机名存放在/etc/hostname文件中,修改主机名时,编辑hostname文件,在文件中输入新的主机名并保存该文件即可 1.打开termini ...
- std::condition_variable(3)复习
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...
- 《从零开始学Swift》学习笔记(Day 15)——请注意数字类型之间的转换
原创文章,欢迎转载.转载请注明:关东升的博客 在C.Objective-C和Java等其他语言中,整型之间有两种转换方法: 从小范围数到大范围数转换是自动的: 从大范围数到小范围数需要强制类型转换,有 ...
- idea 不能编译生成class文件
问题:开发工程中将idea中编译输出目录 out 删掉.发现再次编译不能生成class文件 解决方案:settings -> compiler 勾选自动编译选项
- windows下在Eclipse中启动的tomcat没有乱码,单独部署到tomcat下乱码解决方案
今天遇到了一个很奇怪的问题,在Eclipse中调试,运行项目一切正常,项目的所有编码都是统一的UTF-8.但是在单独部署到tomcat上的时候出现了中文乱码. 解决方案 第一步:确保项目,jsp页面, ...