Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)
题目原文:
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的元素)的更多相关文章
- 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 ...
- 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 ...
- Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list
题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...
- 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 ...
- 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 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- 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 ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- 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 ...
随机推荐
- Oracle 11g 字符集修改
服务端字符集修改 1.确认服务端字符集 select userenv('language') from dual; 2.修改服务端字符集 首先以 DBA 身份登录 Oracle.Windows 系统下 ...
- 使用TransactionTemplate
通过TransactionCallback接口中的方法后(这里用来做业务),将返回值传递到TransactionTemplate的execute()中.通过调用TransactionStatus 的s ...
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- ch12 GUI
<Head First Java 2nd Edition> 摘录 JFrame 代表屏幕上的一个窗口,可以把 buttons, checkboxes, test fields 等等界面相关 ...
- IOC容器Autofac使用
今天我沉浸在IOC的设计模式追求中,听了很多课,也看了很多例子,这个是我的一个测试项目 为了测试代码,我首先准备了两个类一个Car和接口ICar,这两个类和我们平时项目中的DAL与IDAL相似,现在我 ...
- Ural 1036 Lucky Tickets
Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ...
- Tensorflow Eager execution and interface
Lecture note 4: Eager execution and interface Eager execution Eager execution is (1) a NumPy-like li ...
- HDU 2242 连通分量缩点+树形dp
题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...
- POJ——1061 青蛙的约会
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 117858 Accepted: 24599 Descript ...
- vijos 1237 隐形的翅膀
隐形的翅膀 背景 小杉终于进入了天堂.他看到每个人都带着一双隐形翅膀,他也想要. (小杉是怎么看到的?……) 描述 天使告诉小杉,每只翅膀都有长度,两只翅膀的长度之比越接近黄金分割比例,就越完美. 现 ...