My code test right, but run wrong on the OJ


  • 0
    R
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <math.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <limits.h>
    #include <string.h>
    #include <string>
    #include <algorithm>
    #include <iomanip>
    #include <unordered_map>
    
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define read freopen("in.txt","r",stdin)  
    #define write freopen("out.txt","w",stdout)
    using namespace std;
    
    struct length
    {
    	int count;
    	bool left;
    	bool right;
    };
    int longestConsecutive(vector<int> &num) {
            unordered_map<int,length> map;
    		length l;
    		l.count=1;l.left=false;l.right=false;
    		for(int i=0;i<num.size();i++)
    		{
    			if(map.find(num.at(i))==map.end())
    				map[num.at(i)]=l;
    		}
    		unordered_map<int,length>::iterator it;
    		for(it=map.begin();it!=map.end();it++)
    		{
    			if(it->second.left==false)
    			{
    				if(map.find(it->first-1)!=map.end() && map[it->first-1].right==false)
    				{
    					int sumlength=map[it->first].count+map[it->first-1].count;
    					map[it->first].count=sumlength;
    					map[it->first-1].count=sumlength;
    					it->second.left=true;
    					map[it->first-1].right=true;
    				}
    			}
    			if(it->second.right==false)
    			{
    				if(map.find(it->first+1)!=map.end() && map[it->first+1].left==false)
    				{
    					int sumlength=map[it->first].count+map[it->first+1].count;
    					map[it->first].count=sumlength;
    					map[it->first+1].count=sumlength;
    					it->second.right=true;
    					map[it->first+1].left=true;
    				}
    			}
    		}
    
    		it=map.begin();
    		int max=it->second.count;
    		for(it=map.begin();it!=map.end();it++)
    		{
    			if(max<it->second.count)
    				max=it->second.count;
    		}
    		return max;
    }
    int main()
    {
    	int a[]={1,3,5,2,4};
    	vector<int> num(&a[0],&a[5]);
    	cout<<longestConsecutive(num)<<endl;
    	return 0;
    }
    

    I test it right on my local PC, but run wrong on OJ when sample
    Input: [1,3,5,2,4]
    Output: 4
    Expected: 5
    So I don't know if it is correct to define a struct for this submission, my PC get 5, but on OJ, it get 4. I don't know why?


Log in to reply
 

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