c++ solution do not know why past 178/180 cases


  • 0
    Q
    class Solution {
    public:
        bool isPossible(vector<int>& nums) {
            int len = nums.size();
            if(len < 3)
                return false;
            vector<bool> v(len,false);
            vector<int> res;
            int i = 0;
            while(i < len){
                if(v[i] == true){
                    i++;
                    continue;
                }
                int start = nums[i];
                v[i] = true;
                int n = 1;
                start++;
                for(int k=i+1;k<len;k++){
                    if(nums[k] == start && v[k] == false){
                        n++;
                        start++;
                        v[k] = true;
                        if(n==3){
                            res.push_back(start-1);
                            break;
                        }
                    }
                }
                
                if(n<3){
                    int end = start-n;
                    int j;
                    for(j=0;j<res.size();j++){
                        if(res[j] + 1 == end){
                            res[j] = start-1;
                            break;
                        }
                    }
                    if(j == res.size())
                        return false;
                }
                i++;
            }
            return true;
        }
    };
    

    I am so confused why i only passed 178/180 cases....


Log in to reply
 

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