the solution of CountNonDivisible by Codility
question:https://codility.com/programmers/lessons/9
To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn);
Then we iterate array A to get the ith non-divsors by A.size() - count(element) for element in divsor[A[i]] in divsors. Time complexity is O(n*?
); ?
represent the average of divsors
this method unsatisfy the time requirement , for two test case get TIMEOUT error. NEED IMPROVE IT LATER.
code:
#include <algorithm>
#include <map>
//this method not fast enough
vector<int> solution(vector<int> &A) {
// write your code in C++11
map<int,int> dic;
map<int,vector<int> > divsors;
int size = A.size();
int max = *max_element(A.begin(),A.end());
for(int i=0; i<size; i++){
dic[A[i]]++;
if(divsors.count(A[i])==0){
vector<int> vec(1,1);
divsors.insert(make_pair(A[i],vec));
}
} for(int i=2; i<= max; i++){
int element = i;
while(element <=max){
if(divsors.count(element)!=0 && find(divsors[element].begin(),divsors[element].end(),i)==divsors[element].end()){
divsors[element].push_back(i);
}
element+=i;
}
}
vector<int > res;
for(int i=0; i<size; i++){
vector<int> t = divsors[A[i]];
int cnt = size;
for(int j=0; j<t.size(); j++){
cnt -= dic[t[j]];
}
res.push_back(cnt);
}
return res;
}
the solution of CountNonDivisible by Codility的更多相关文章
- Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...
- Solution to Triangle by Codility
question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
随机推荐
- day03_12/13/2016_bean属性的设置之setter方法注入
- B-Tree 漫谈 (从二叉树到二叉搜索树到平衡树到红黑树到B树到B+树到B*树)
关于B树的学习还是需要做点笔记. B树是为磁盘或者其他直接存取辅助存储设备而设计的一种平衡查找树.B树与红黑树的不同在于,B树可以有很多子女,从几个到几千个.比如一个分支因子为1001,高度为2的B树 ...
- Bootstrap3模态框Modal垂直居中样式
1,Bootstrap 模态框插件Bootbox垂直居中样式: <!DOCTYPE html> <html lang="en"> <head> ...
- Angular——事件指令
基本介绍 angular的事件指令都是ng-click,ng-blur....的形式,类似于js的事件 基本使用 <!DOCTYPE html> <html lang="e ...
- JS——event
触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件有关的信息: 普通浏览器支持 event(传参),IE678支持 window.event(无参),兼容写法: < ...
- vim之补全2(完全个人定制版)
关于补全的方面要说的的确很多, 这里选择分为两个章叙述. 如果你想学vim, 你需要有很强的耐心, 如果你想锻炼这种耐心, 你可以试着先看完我之前的文章. 好了, 下面继续我们的vim补全吧. vim ...
- Tcl之Lab1
Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...
- NBXplorer的配置
首先,必须安装bitcoin core bitcoin core启动时,会提示你定义数据存放目录,在数据存放目录下,找到bitcoin.conf文件,并填写内容: server=1rpcuser=rp ...
- centos 7 配置nginx
安装nginx: curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0. ...
- CentOS下使用yum安装配置和使用svn
安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 ? 1 2 3 4 5 6 7 8 9 1 ...