【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 ...
随机推荐
- LeetCode447. Number of Boomerangs
Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...
- [搬家]新域名 akagi201.org
现在感觉自己做了好多年的垃圾信息制造者 以后只在网络上发布有用的东西, 垃圾或者对别人没用的东西就放到自己的硬盘上把 http://akagi201.org
- 实体框架迁移,EntityFramework
主要就是这个:http://msdn.microsoft.com/zh-cn/data/jj591621.aspx 下面这个是写得不错的,比较详细: 首先打开工具--->>>库程序包 ...
- sql 注入安全过滤-安全模块
<?php /** * 安全模块 * Email:zhangyuan@tieyou.com * 主要针对xss跨站攻击.sql注入等敏感字符串进行过滤 * @author hkshadow */ ...
- JDK的命令具体解释操作
JDK的命令具体解释1 rmic 功能说明: rmic 为远程对象生成 stub 和 skeleton. 语法: rmic [ options ] package-qualified-class-na ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- The Best Hacking Tools
The Best Hacking Tools Hacking Tools : List of security tools specifically aimed toward security pro ...
- POJ 1408 Fishnet【枚举+线段相交+叉积求面积】
题目: http://poj.org/problem?id=1408 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- Echarts-雷达图
// 显示能力雷达图 $(".company .grade").hover(function () { $(".powerChart").show(); var ...
- Qt里的原子操作QAtomicInteger
所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...