--DAY 19--
Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
Problem: Picking Numbers
Detail: here
My solution(javascript):
- Using sort:
function PickingNumbers(){
let max=1,tmp=1,idx=0;
for(let i=1;i<a.length;i++){
if(a[i]-a[idx]<=1) tmp++;
else{
idx=i;
tmp=1;
}
max=(tmp>max)?tmp:max;
}
return max;
}
- Using hash map:
function pickingNumbers(a) {
let max=1;
let map = new Map();
for(let number of a){
if(map.has(number)){
map.set(number, map.get(number)+1);
}
else map.set(number, 1);
}
for(const [key,value] of map){
let tmp=value;
if(map.has(key+1)) tmp=map.get(key)+map.get(key+1);
if(tmp>max) max=tmp;
}
return max;
}
-->If you have better solution or any question, please comment below. I will appreciate.
Top comments (0)