tejas2's blog

By tejas2, history, 15 months ago, In English

This is my submission for 3Sum Problem on Leetcode and the complexity comes out to be O(n^2), since I have used a nested for loop for the length of the nums array (nums.length()<=3000). However, I am getting TLE on all zeros case. Can anyone help me understand what is wrong with my complexity? The code file with the testcase is attached.

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        set<vector<int>> ans;
        vector<int> present(200001,0);
        int n = nums.size();
        for(int x=0;x<n;x++)
        {
            int val = nums[x];
            int ind = val + 100000;
            present[ind]++;
        }

        for(int x=0;x<n;x++)
        {
            int v1 = nums[x];
            present[v1+100000]--;
            for(int y=0;y<n ;y++)
            {
                if(y==x)
                {
                    continue;
                }
                int v2 = nums[y];
                present[v2+100000]--;
                int rem = -v1-v2;
                
                if(rem>=-100000 && rem<=100000 && present[rem+100000]>0)
                {
                    vector<int> pans;
                    pans.push_back(v1);
                    pans.push_back(v2);
                    pans.push_back(rem);
                    sort(pans.begin(),pans.end());
                    if(v1+v2+rem==0)
                    {
                        // cout<<"inserting : {"<<v1<<" "<<v2<<" "<<rem<<"}"<<endl;
                        ans.insert(pans);
                    }
                }
                present[v2+100000]++;
            }
            present[v1+100000]++;
        }
        vector<vector<int>> ret;
        for(auto i : ans)
        {
            ret.push_back(i);
        }
        return ret;
    }
};

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By tejas2, history, 17 months ago, In English

This is my submission 208898289 for the problem 1702E — Split Into Two Sets (1702E - Раздели на два набора). I am getting runtime error on testcase-2, and the Diagnostics Hint in the Verdict shows index-out-of-bounds for visited array. I am unable to find any reason for this happening. Also, I tried submitting the solution increasing the size of visited array by 10 (208897546), but I am getting RTE again. Can someone help?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By tejas2, history, 18 months ago, In English

Following is my solution for 977E - Cyclic Components (Finding number of cyclic connected components in a graph) :- My Solution : 207324313. Can anyone please tell why am I getting MLE?

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it