๋ฌธ์ ๐ฝ
https://school.programmers.co.kr/learn/courses/30/lessons/138476
๋ค๋ฅธ์ฌ๋ ํ์ด ๐ฝ
function solution(k, tangerine) {
let answer = 0;
const tDict = {};
tangerine.forEach((t) => tDict[t] = (tDict[t] || 0) + 1);
const tArr = Object.values(tDict).sort((a, b) => b - a);
for (const t of tArr) {
answer++;
if (k > t) k -= t;
else break;
}
return answer;
}
๋ดํ์ด๐ฝ
1. ์ฑ๊ณต โญ
function solution(k, tangerine) {
let object ={}
tangerine.forEach((value)=>{
if(object[value]){
++object[value];
}else{
object[value]=1;
}
})
let answer=0
let sum=0
let valueArray = Object.values(object).sort((a,b)=>{ return b-a})
for(let i=0; i<valueArray.length; i++){
++answer
if(sum+valueArray[i]>=k){
break;
}
sum+=valueArray[i]
}
return answer;
}
๋๋์ ๐ฝ
Object.keys(object) ์ Object.values(object) ์ ์ฌ์ฉ๋ฒ์ ๋ํด ์์งํ ์ ์๋ ์ข์ ๊ธฐํ์๋ค.