0 ms C++ solution time limit exceeded


  • 0
    B

    I ran into a TLE, and when I copied the test case and pasted it into the custom test-case, it runs fine and states it has a run time of 0 ms. Don't know why I'm getting a TLE...

    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& nums) {
            int length = nums.size();
            vector<int>process;
            unordered_map<int,int>check;
            for(int i =0;i < length; i++){
                if(check[nums[i]]<3){
                    process.push_back(nums[i]);
                    check[nums[i]]++;
                }
            }
            nums = process;
            length = nums.size();
            sort(nums.begin(),nums.end());
            
            set<vector<int>>nodups;
            for(int i = 0; i<length-2; i++){
                if(nums[i]>0)
                    break;
                int start = i+1;
                int end = length -1;
                //cout<<nodups.size()<<endl;
                while(start < end){
                    int sum = nums[i] +nums[start]+nums[end];
                    if(sum ==0)
                    {
                        nodups.insert({nums[i],nums[start],nums[end]});
                        start++;
                        end--;
                        //break;
                    }
                    else if(sum < 0)
                        start++;
                    else 
                        end--;
                    
                }
                
            }
            int size = nodups.size();
            vector<vector<int>>sol(size,vector<int>(3));
            set<vector<int>>::iterator it;
            int i =0;
            for(it = nodups.begin();it != nodups.end(); it++){
                sol[i] = (*it);
                i++;
            }
            return sol;
        }
    };

Log in to reply
 

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