TLE for the solution... Please help


  • 0
    V

    Could you please help me in analyzing the run time complexity of this solution ? Why am i getting TLE ? I know Sieve solution is optimal. But I would like to understand the runtime of this code ?

    class Solution {
    public:
        int countPrimes(int n) {
            vector<int> primeList;
            bool isPrime = true;
            
            for(int i=2; i<n; i++) {
                for(int j=0; j<primeList.size(); j++) {
                    if((i % primeList[j]) == 0) {
                        isPrime = false;
                        break;
                    }
                }
                if(isPrime == true) {
                    primeList.push_back(i);
                }
                isPrime = true;
            }
            return(primeList.size());
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.