Leetcode 659. Split Array into Consecutive Subsequences

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Great video as always man. I tried using a different approach and it passed 186/187 testcases and I feel like the logic is correct. Here is the code, any suggestions are appreciated:-
class Solution {
public:
bool isPossible(vector<int>& arr) {
unordered_map<int, int> m;
int n=arr.size();
for(int i=0;i<n;i++){
m[arr[i]]++;
}
int size=m.size();
for(int i=0;i<n;i++){
if(m[arr[i]]>0){
if((m.find(arr[i]+1)!=m.end() && m[arr[i]+1]>0) && (m.find(arr[i]+2)!=m.end() && m[arr[i]+2]>0)){
m[arr[i]+1]--;
m[arr[i]]--;
}
else return false;}
else if(m[arr[i]]==0){
if(( m.find(arr[i]+1)!=m.end() && m[arr[i]+1]>0) | ((m.find(arr[i]-2)!=m.end() && m.find(arr[i]-1)!=m.end()) && (m[arr[i]-1]==0 && m[arr[i]-2]==0))){
if( m[arr[i]+1]>0) m[arr[i]+1]--;
}
else return false;
}
else return false;
}
return true;
}
};

ravirajshelar
welcome to shbcf.ru