public boolean isPossible(int[] nums) {
TreeMap<Integer,Integer>map=new TreeMap();
Queue<Integer>pq=new PriorityQueue();
List<Integer>list=new ArrayList();
for(int i:nums){
if(map.containsKey(i)){
map.put(i,map.get(i)+1);
}
else{
map.put(i,0);
list.add(i);
}
}
while(true){
for(Integer key:map.keySet()){
pq.add(key);
map.put(key,map.get(key)-1);
if(map.get(key)==0){
try{
list.remove(key);
}
catch(NullPointerException e){
continue;
}
finally{
continue;
}
}
}
if(pq.size()<3) return false;
pq.clear();
if(list.isEmpty()) break;
}
return true;
}
}Above code is my own implementation for Sum 659.
what I tried to do is add elements into the priority queue brute forcely and the if the priority Queue size is than 3 i returned false;
when i tried above approach Time limit is exceeded.
I sure could use some help.