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,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
随机推荐
- [Android]异常6-TextView setText延迟显示
背景:Thread和Handler显示数据到界面 解决办法有: 解决一>界面使用了ListView.GridView等,把高度和宽度调整为固定值或者match_parent 解决二>某处U ...
- PHP中单例模式与工厂模式
单例模式概念 单例模式是指整个应用中类只有一个对象实例的设计模式. 单例模式的特点 一个类在整个应用中只有一个实例 类必须自行创建这个实例 必须自行向整个系统提供这个实例 php中使用单例模式的原因 ...
- 3--Java NIO基础1
一.NIO概述 1. BIO带来的挑战 BIO即堵塞式I/O,数据在写入或读取时都有可能堵塞,一旦有堵塞,线程将失去CPU的使用权,性能较差. 2. NIO工作机制 Java NIO由Channel. ...
- SQL基本操作——HAVING
HAVING:在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 我们拥有下面这个 "Orders" 表: O_Id OrderDate Or ...
- Centos6.4 安装fail2ban防暴力破解
Centos6.4 安装fail2ban防暴力破解 一. 安装 curl -O https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0 m ...
- Xamarin.Forms实现touch事件
Xamarin.Forms的View没有touch事件,只能自己实现 首先,在共享项目里面,放入这几个类,结构大概是这样的: using System; using Xamarin.Forms; na ...
- 用u盘安装黑苹果10.12.3
链接: https://pan.baidu.com/s/1eR9GgwE 密码: rubh 主机和显示器必须是数字口连接,如dvi.displayport,VGA不能进安装界面 下载苹果镜像文件10. ...
- CAD导出黑白色的pdf(com接口)
主要用到函数说明: IMxDrawModifyTheColor 接口 用来修改图面所有对象的颜色,把它的颜色都修改成一个指定的值. IMxDrawModifyTheColor::Do 修改颜色,详细说 ...
- js的加法操作表
Number + Number -> 加法 Boolean + Number -> 加法 Boolean + Boolean -> 加法 Number + String -> ...
- php银联支付
简介 PHP银联支付 流程 1.注册 银联 - 技术开发平台和商户服务平台 https://open.unionpay.com 注意:注册时建议使用IE浏览器,之前注册时插件老是用不了,使用IE10以 ...