Logo
Hyunsu Blog
algorithm-greedy

๐Ÿ“†Published :Mar 2, 2024 โ€ข

๐Ÿ“†Updated :Mar 2, 2024 โ€ข

โ˜•๏ธ1min

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๊ณจ๋“ ๋ž˜๋น— ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ํ•ฉ๊ฒฉ์ž ๋˜๊ธฐ ํŒŒ์ด์ฌ ํŽธ์˜ 15์žฅ ์จ๋จธ๋ฆฌ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰ ์ฑ•ํ„ฐ์ด๋‹ค. ๊ฐ™์ด ์Šคํ„ฐ๋””ํ•œ ๋ฉค๋ฒ„๋“ค๊ณผ ํŒ€์žฅ๋‹˜๊ป˜ ๊ณ ๋งˆ์šด ์ธ์‚ฌ๋ฅผ ์˜ฌ๋ฆฐ๋‹ค.
  • ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€๋ฉด์„œ greedy์— ์ต์ˆ™ํ•ด ์ง€๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค.

๋ชธํ’€๊ธฐ ๋ฌธ์ œ

๊ฑฐ์Šค๋ฆ„๋ˆ ์ฃผ๊ธฐ

๊ฑฐ์Šค๋ฆ„๋ˆ amount๊ฐ€ ์žˆ์„ ๋•Œ ํ™”ํ๋‹จ์œ„ [1,10,50,100] ์„ ์ตœ์†Œํ•œ์˜ ์‚ฌ์šฉํ•œ ํ™”ํ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” solution() ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”. ์ œ์•ฝ์กฐ๊ฑด

  • ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์˜ ํ™”ํ๋‹จ์œ„๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ

ํ™”ํ๋‹จ์œ„๋ฅผ ์ตœ์†Œํ•œ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„ , ํฐ ๋‹จ์œ„์˜ ํ™”ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฑฐ์Šค๋ฆ„๋ˆ์„ ์ค€๋‹ค.

์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ๋ถ€๋ถ„์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์œผ๋ฏ€๋กœ O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

function change_coin(amount){ const answer = []; const changes = [1, 10, 50, 100] changes.sort((a,b)=> b-a) for(const c of changes){ const count = Math.floor(amount/c); amount = amount % c; for(let i=0; i<count; i++){ answer.push(c) } } return answer } console.log(change_coin(123)) console.log(change_coin(350))

๋ถ€๋ถ„ ๋ฐฐ๋‚ญ ๋ฌธ์ œ

๋ฌธ์ œ์„ค๋ช… ๋ฌด๊ฒŒ์™€ ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ์ง items์™€ ๋ฐฐ๋‚ญ weight_limit์ด ์ฃผ์–ด์งˆ ๋•Œ, ๋ถ€๋ถ„ ๋ฐฐ๋‚ญ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” solutionํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ์ œ์•ฝ์กฐ๊ฑด 1<=weight_limit <=10,000 1<=items.length<=1,000

  • ์ฃผ์–ด์ง„ ๋ฌด๊ฒŒ ์ œํ•œ์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ฐ€์น˜๊ฐ€ ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ๋ฌผ๊ฑด์„ ๋„ฃ๋Š” ๋ฌธ์ œ
  • ๊ฐ ๋ฌผ๊ฑด์€ ๋ฌด๊ฒŒ์™€ ๊ฐ€์น˜๋กœ ํ‘œํ˜„๋œ๋‹ค.
  • ๋ฌผ๊ฑด์€ ์ชผ๊ฐค ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฌผ๊ฑด์˜ ์ผ๋ถ€๋ถ„์ด ๋‹ด๊ธธ ์ˆ˜ ์žˆ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ •๋ ฌ๋กœ ์ธํ•ด O(nlogn) ์ด ๋œ๋‹ค.
function fractional_knapsack(items, limit) { let total_v = 0; // ๋ฌด๊ฒŒ๋‹น ๊ฐ€์น˜ ๊ณ„์‚ฐ items.forEach((item) => item.push(item[1] / item[0])); // ๋ฌด๊ฒŒ๋‹น ๊ฐ€์น˜๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ items.sort((a, b) => b[2] - a[2]); for (const [w, v, _v] of items) { if (w <= limit) { total_v += v; limit -= w; } else { total_v += _v * limit; break; } } return Math.round(total_v * 100) / 100; } console.log( fractional_knapsack( [ [10, 19], [7, 10], [6, 10], ], 15, ), ); //27.33 console.log( fractional_knapsack( [ [10, 60], [20, 100], [30, 120], ], 50, ), ); //240

์˜ˆ์‚ฐ ๋ฌธ์ œ ์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฌธ์ œ์„ค๋ช… ๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด d์™€ ์˜ˆ์‚ฐ budget์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ๋Œ€ ๋ช‡ ๊ฐœ์˜ ๋ถ€์„œ์— ๋ฌผํ’ˆ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ d๋Š” ๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์ด๋ฉฐ, ๊ธธ์ด(์ „์ฒด ๋ถ€์„œ์˜ ๊ฐœ์ˆ˜)๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค. d์˜ ๊ฐ ์›์†Œ๋Š” ๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋ถ€์„œ๋ณ„ ์‹ ์ฒญ ๊ธˆ์•ก์€ 1 ์ด์ƒ 100,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. budget์€ ์˜ˆ์‚ฐ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1 ์ด์ƒ 10,000,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

  • ์ตœ๋Œ€ ๋งŽ์€ ๋ถ€์„œ์— ๋ฌผํ’ˆ ์ง€์›์„ ํ•˜๊ธฐ ์œ„ํ•ด์„  ์ ๊ฒŒ ์š”์ฒญํ•œ ์˜ˆ์‚ฐ ์ˆœ์œผ๋กœ ๋จผ์ € ๊ณ„์‚ฐํ•œ๋‹ค.
  • ์˜ˆ์‚ฐ์ด ๋‚จ์•„์žˆ๋Š” ๋™์•ˆ ์š”์ฒญํ•œ ์˜ˆ์‚ฐ์„ ๋”ํ•จ
  • ํ˜„์žฌ ์‚ฌ์šฉํ•œ ์˜ˆ์‚ฐ์ด ์ฃผ์–ด์ง„ ์˜ˆ์‚ฐ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ถ€์„œ์˜ ์ˆ˜++
  • ํ˜„์žฌ ์‚ฌ์šฉํ•œ ์˜ˆ์‚ฐ์ด ์ฃผ์–ด์ง„ ์˜ˆ์‚ฐ ๋ณด๋‹ค ํฌ๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„ O(nlogn)
function solution(d, budget) { return d .sort((a, b) => a - b) .reduce((cnt, b) => { return (cnt += (budget -= b) >= 0); }, 0); } console.log(solution([1, 3, 2, 5, 4], 9)); //3 console.log(solution([2, 2, 3, 3], 10)); //4

๊ตฌ๋ช…๋ณดํŠธ ๋ฌธ์ œ ์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

  • ์‰ฌ์šด๋“ฏ ํ•˜๋ฉด์„œ๋„ ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ์ ‘๊ทผ ํ•  ๋•Œ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด 2๋ช…์„ weight์— ๊ฐ€์žฅ ๊ฐ€๊น๊ฒŒ ์ฑ„์›€์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ „๋žต์ด ์‰ฝ๊ฒŒ ๋– ์˜ค๋ฅด์ง„ ์•Š์•˜๋‹ค.
  • ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋กœ ์ตœ๋Œ€ 2๋ช…์ด๊ณ , limit์— ์ตœ๋Œ€ํ•œ ๊ฐ€๊น๊ฒŒ ๋งŒ๋“ค๊ธฐ ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ๊ณผ ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ์„ ๊ฐ™์ด ํƒœ์šด๋‹ค.
function solution(people, limit) { var answer = 0; let left = 0; let right = people.length-1; people.sort((a,b)=>a-b) //ํˆฌํฌ์ธํ„ฐ while(left<right){ // ๋‘๋ช…์ด ๊ฐ™์ด ํƒˆ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด : ๋‘˜์˜ ํ•ฉ์ด limit๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผํ•จ // ๊ฐ™์ด ํƒˆ ์ˆ˜ ์žˆ์œผ๋ฉด left์™€ right๋ฅผ ํ•œ์นธ์”ฉ ์˜ฎ๊ฒจ์คŒ if(people[left] + people[right] <= limit){ answer+=1 left+=1; right-=1; }else{ //๊ฐ™์ด ํƒˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ : limit์„ ๋„˜๋Š” ๊ฒฝ์šฐ // ๋” ์ž‘์€ ์‚ฌ๋žŒ์„ ํƒœ์›Œ์•ผ ํ•จ . // right๋ฅผ ์˜ฎ๊ธฐ๋ฉด์„œ, ํฐ ์‚ฌ๋žŒ์€ ํ˜ผ์ž ํƒ€๊ฒŒ ํ•จ right-=1; answer+=1 } } // ๋งˆ์ง€๋ง‰ ๋‚จ์€ ํ•œ๋ช… ํƒœ์šฐ๊ธฐ if(left===right){ answer+=1 } return answer } console.log(solution([70, 50, 80, 50],100)) //3 console.log(solution([70, 80, 50],100)) //3

๊ทค๊ณ ๋ฅด๊ธฐ ๋ฌธ์ œ ์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฌธ์ œ์„ค๋ช… ๊ฒฝํ™”๊ฐ€ ํ•œ ์ƒ์ž์— ๋‹ด์œผ๋ ค๋Š” ๊ทค์˜ ๊ฐœ์ˆ˜ k์™€ ๊ทค์˜ ํฌ๊ธฐ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด tangerine์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฒฝํ™”๊ฐ€ ๊ทค k๊ฐœ๋ฅผ ๊ณ ๋ฅผ ๋•Œ ํฌ๊ธฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. 1 โ‰ค k โ‰ค tangerine์˜ ๊ธธ์ด โ‰ค 100,000 1 โ‰ค tangerine์˜ ์›์†Œ โ‰ค 10,000,000

  • ์ข…๋ฅ˜์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŽ์€ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ข…๋ฅ˜ ๋ถ€ํ„ฐ ์„ ํƒ ํ•œ๋‹ค.
function solution(k, tangerine) { let cnt =0; // mapper const mapper = {} //๊ทค์˜ ์ข…๋ฅ˜๋ณ„๋กœ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ tangerine.forEach((t)=> mapper[t]=(mapper[t]||0)+1) // sort ๋‚ด๋ฆผ์ฐจ์ˆœ - ์ข…๋ฅ˜์˜ ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ข…๋ฅ˜๋ถ€ํ„ฐ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด const sortedMapper =Object.entries(mapper).sort((a,b)=>b[1]- a[1]) // loopํ•˜๋ฉด์„œ for (const [_, t] of sortedMapper){ k-=t // ๊ทค ๋‹ด๊ธฐ cnt+=1 // ์ข…๋ฅ˜ ์ˆ˜ ์ฆ๊ฐ€ if(k<=0) break // ๊ทค ๋‹ค ๋‹ด์•˜์œผ๋ฉด ์ข…๋ฃŒ } return cnt; } console.log(solution(5, [3, 1, 4, 1, 5])) //3 console.log(solution(6, [1, 3, 2, 5, 4, 5, 2, 3])) //3

๊ธฐ์ง€๊ตญ ์„ค์น˜

Hi, I'm Hyunsu ๐Ÿ‘‹

Profile Image

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ฃผํ˜„์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ’๋ถ€ํ•˜๊ณ  ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ์ผ์— ๋ฟŒ๋“ฏํ•จ์„ ๋Š๋‚๋‹ˆ๋‹ค.

์˜ต์‹œ๋””์–ธ(Obsidian)์—์„œ ํ˜„์žฌ ๋ธ”๋กœ๊ทธ๋กœ ํ•˜๋‚˜์”ฉ ๊ธ€์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์— ์žˆ์–ด์š”. โ˜•๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป โ›ท

Github on ViewReach Me Out