All test cases passed but showing "Memory Limit Exceede".Please Help :)


  • 0
    C

    *This is my solution for 561. Array Partition I.83/83 Test cases passes,but it is showing me Memory Limit Exceeded.Could anyone please help me here? Thank you :)

        map<int,int> mp;
        int i;
        int n = nums.size();
        
        for(i = 0;i<n;i++)
            mp[nums[i]]++;
        
        int sum = 0;
        
        int flag = 0;
        int prev = 0;
        
        for(i=-10000;i<=10000;i++){
            
            if(mp[i]>0)
            
            if(flag == 0){
                
                if(mp[i]%2 == 0){
                    
                    sum = sum + i * (mp[i]/2);
                    prev = 0;
                    flag = 0;
                    
                }else if(mp[i]%2 != 0){
                    
                    sum = sum + i * (mp[i]/2);
                    flag = 1;
                    prev = i;
                    
                }
                
            }
                
                
            else{
                
                if(mp[i]%2 != 0){
                    
                    flag = 0;
                    sum = sum + prev + i * (mp[i]/2);
                    prev = 0;
                    
                }else{
                    
                    flag = 1;
                    sum = sum + prev + i * ((mp[i]-1)/2);
                    prev = i;
                    
                }
                
                
            }
        }
            
        
        return sum;

Log in to reply
 

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