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 ...
随机推荐
- console.log格式化及console对象
一.console.log格式化打印 console.log格式化这一用法一般都在个人博客或其他官网上有,当F12查看网页元素时,在控制台(console)那里偶尔会发现一些个性化的输出,感觉很奇特很 ...
- Oracle创建用户、角色、授权、建表空间
oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- CRC校验算法学习
原文:http://www.repairfaq.org/filipg/LINK/F_crc_v31.html 本文根据上述链接原文翻译而来,如有错误,忘广大网友互相帮忙纠正,谢谢! 1.前言: 1.0 ...
- java,有用的代码片段
在我们写程序的过程中,往往会经常遇到一些常见的功能.而这些功能或效果往往也是相似的,解决方案也相似.下面是我在写代码的过程中总结的一些有用的代码片段. 1.在多线程环境中操作同一个Collection ...
- 校长的收藏(洛谷 U4534)
题目背景 XS中学的校长喜欢收集手办,家里面都是价值不菲的手办. 校长喜欢给手办们排队并且对于某些些区间内的手办喜爱有加. 现在,校长外出散步(找乐子),你潜入他的房间打算借(偷走)他的手办炫耀一下. ...
- jsp内置对象(转)
JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...
- F - Piggy-Bank 完全背包问题
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. ...
- Drools等规则引擎技术对比分析
项目中需要设计开发一个规则引擎服务,于是调研了业界常用的规则引擎. 常见的规则引擎如下: Ilog JRules 是最有名的商用BRMS: Drools 是最活跃的开源规则引擎: Jess 是Clip ...
- 洛谷(cogs 1293/bzoj 1212) P2292 [HNOI2004]L语言
1293. [HNOI2004] L语言 ★★★ 输入文件:language.in 输出文件:language.out 简单对比时间限制:1 s 内存限制:162 MB [题目描述] ...