题目原文:

Decimal dominants. Given an array with n keys, design an algorithm to find all values that occur more than  n/10 times. The expected running time of your algorithm should be linear.

分析:

直观上将n个元素遍历一遍,并记录每个元素出现的次数就可以实现,虽然时间复杂度是O(n),但是空间复杂度却高达n,这肯定不是该题目的初衷。对于n个元素来说,出现n/10次的元素最多有10个,那么出现超过n/10次的元素最多不超过9个,所以需要9个额外空间auxs就能满足需求。

这9个辅助空间aux怎么使用呢?可采用俄罗斯方块的消去一行的思路。只不过这里消去一行的情况是该行中元素各不相同。

1. 遍历数组array中的每个元素array[i]

2. 如果array[i]在aux中存在,将其在aux中的计数+1

3. 如果array[i]在aux中不存在

  3.1 如果aux未满,将其放入aux中,并记录其个数为1

  3.2 如果aux已满,将aux中已经存在的各个元素的计数都减去1,直到某个元素的个数变成0,将array[i]放入aux中该位置处,并记录其个数为1

4. 出现次数超过n/10的元素在array遍历完了之后,还会继续存在于aux中,当然aux中可存在着位于array后方但出现次数不满足要求的元素。这时只需要遍历aux的同时再遍历一遍array,记录aux中各个元素在array中出现的次数,将其中出现次数真正超过n/10的元素找出来即可。

 package week3;

 import java.util.ArrayList;
import java.util.Arrays;
import edu.princeton.cs.algs4.StdRandom; public class ElemsMoreThanNDivTenTimes { private class Element{//辅助空间元素定义,用来记录元素值及其出现次数
public int element;
public int count;
public Element(int e,int c){
this.element = e;
this.count = c;
}
};
private Element[] elems = new Element[9]; //申请9个辅助空间 public ArrayList<Integer> findElements(int[] arrays){
int n = arrays.length;
for(int k=0;k<9;k++){
elems[k] = new Element(0,0); //辅助空间初始化
}
for(int i=0;i<n;i++){
int index = findIndex(arrays[i]);
if(index >= 0)
elems[index].count ++;
else
addToElems(arrays[i]);
}
return verifyElems(arrays);
} private int findIndex(int e){
for(int k = 0; k<9;k++){
if(elems[k].element == e)
return k;
else if(elems[k].count == 0){
elems[k].element = e;
return k;
}
}
return -1;
}
private void addToElems(int e){
boolean insertFlag = false;
while(!insertFlag){
for(int k=0; k<9;k++){
elems[k].count --;
if(elems[k].count <= 0){
elems[k].element = e;
elems[k].count = 1;
insertFlag = true;
break;
}
}
}
}
private ArrayList<Integer> verifyElems(int[] arrays){
int n = arrays.length;
for(int k = 0; k< 9; k++){
elems[k].count = 0;
for(int i = 0; i< n;i++){
if(arrays[i]==elems[k].element)
elems[k].count++;
}
}
ArrayList<Integer> elemList = new ArrayList<Integer>();
for(int k = 0; k< 9; k++){
if(elems[k].count > n/10)
elemList.add(elems[k].element);
}
return elemList;
} public static void main(String[] args){
int n = 20;
int[] array = new int[n];
for(int i=0;i<n;i++){
array[i] = StdRandom.uniform(n);
}
System.out.println(Arrays.toString(array));
ElemsMoreThanNDivTenTimes elems = new ElemsMoreThanNDivTenTimes();
ArrayList<Integer> elemList = elems.findElements(array);
System.out.println(elemList.toString());
}
}

Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)的更多相关文章

  1. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  2. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  3. Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list

    题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...

  4. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  5. Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array

    题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...

  6. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  7. Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time

    题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...

  8. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

随机推荐

  1. luogu P3899 [湖南集训]谈笑风生 线段树合并

    Code: #include<bits/stdc++.h> #define maxn 300002 #define ll long long using namespace std; vo ...

  2. BigDecimal舍入规则

    1.ROUND_UP 舍入远离零的舍入模式. 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1). 注意,此舍入模式始终不会减少计算值的大小. 2.ROUND_DOWN 接近零的舍入模 ...

  3. nginx代理标准配置

    #nginx开启的进程数worker_processes   4;     #4核CPU   #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log  ...

  4. BZOJ 5106 [CodePlus2017]汀博尔

    [题解] 二分答案.r要设好,不能随便设置为max(s,len),不然check的时候会爆long long #include<cstdio> #include<algorithm& ...

  5. unigui导出TMS.Flexcel【5】

    参考代码 procedure TUniFrmeWebEmbedBase.ExportData; //导出到excel var FlexCelImport1: TExcelFile; i, rowind ...

  6. 《Spring Boot 那些事》

    <Spring Boot 那些事>----https://www.bysocket.com/?p=1124

  7. git删除远程remote分支

    git 命令如下: git push origin --delete <远程分支名字>

  8. hdu 5037 模拟网选1006

    /* 模拟 实例: 33 1 10 5 5 2 10 3 3 6 1 3 2 1 1 4 2 1 1 5 2 1 1 6 2 1 1 7 2 1 5 20 8 1 2 3 4 5 1 20 8 5 0 ...

  9. Spring @Conditional注解 详细讲解及示例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xcy1193068639/article/details/81491071 前言: @Conditi ...

  10. codevs—— 1077 多源最短路

    1077 多源最短路  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 已知n个点(n<=100),给你 ...