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;
}
时间: 2024-10-05 04:19:32

the solution of CountNonDivisible by Codility的相关文章

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

Codility MaxAbsSliceSum Solution

1.题目(HARD) A non-empty zero-indexed array A of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. Thesum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q]. A min abs slice is a slice whos

Codility NumberSolitaire Solution

1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from 0 to N − 1. There is a number written on each square. A non-empty zero-indexed array A of N integers contains the numbers written on the squares. More

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

codility上的问题(34) Fluorum 2014

好久没写codility的题了,一来没时间,二来有的题目不太好分析.这个题比较有意思,我还没有给出非常严格的证明. 给定一棵树(无向无环图),从一个节点出发,每次选择一个节点,从起点到目的节点的路径上没经过的节点尽可能多,直到遍历完所有的节点.如果起点到两个目的节点的路径中没经过的节点同样多,则选择标号较小的节点作为目的节点.如此继续,直到遍历所有的节点,求按顺序选择了哪些目的节点? 例如从2 开始,第一次选择0,然后1,0变为经历过的节点. 然后从0开始,第二次选择6, 然后4,6变为经历过的

*[codility]Country network

https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave