the solution of CountNonDivisible by Codility
来源:互联网 发布:杭州恩牛网络借壳上市 编辑:程序博客网 时间:2024/06/03 23:10
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;}
0 0
- the solution of CountNonDivisible by Codility
- Solution of NumberOfDiscIntersections by Codility
- Codility-CountNonDivisible
- codility CountNonDivisible
- Solution of Codility
- Solution to Triangle by Codility
- The solution of ' The project type is not supported by this installation?'
- 解决方案: the selected file is a solution file but was created by a newer version of this application an
- The solution of weblogic webservice Exception.
- Solution--Integration of PCC in the IMS
- solution Of 1069. The Black Hole of Numbers (20)
- A problem during the compilation of Lemur and the solution
- The solution on the Elements of Statistical Learning ( Ex. 8)
- PetStore - Solution of fixing the "item.attribute" exception
- The solution of Unable to install breakpoint with Eclipse debugger
- solution to bash error (because of the PATH)
- Solution of Could not download the Silverlight application
- SharePoint solution Error: Misconfiguration of the Microsoft SharePoint State Service
- Hadoop之——分布式集群安装过程简化版
- Matlab之统计函数
- jsp ,servlet中路径
- 简单利用CodeDom动态编译的例子
- Collection下的List接口
- the solution of CountNonDivisible by Codility
- 线程的两种创建方式:Thread类和Runnable接口,它们都要复写run方法
- android intent打开各种文件的方法
- SQL中INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN区别
- 欢迎使用CSDN-markdown编辑器
- NSString笔记与OC"命名空间"
- 5.2-4
- 15HD_OJ——超级楼梯(斐波那契数列)
- 杂七杂八 - CSS选择器