๐Ÿ’ป ์ด์ง„ ํƒ์ƒ‰ by ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์—ฐ์Šต๋ฌธ์ œ

waterglassesยท2022๋…„ 3์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
5/50
post-thumbnail

ํŠธ๋ฆฌ์˜ ๊ฐœ๋…๊ณผ ์ˆœํšŒ, ํž™, ํŠธ๋ผ์ด, ์ •๋ ฌ, ์ด์ง„ ํƒ์ƒ‰์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋‹ค. ์ •๋ ฌ ๋ฌธ์ œ ๊ฐ€์žฅ ํฐ ์ˆ˜ ์—์„œ๋„ 1์‹œ๊ฐ„๋™์•ˆ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์‹คํŒจ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ณด๋ƒˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ์ •๋ ฌ ๋ฌธ์ œ ๋ณด๋‹ค๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ์ž…๊ตญ ์‹ฌ์‚ฌ ๋ฌธ์ œ๋ฅผ ํšŒ๊ณ ํ•˜๊ณ  ์‹ถ์–ด์„œ ์ ๋Š”๋‹ค.

๐Ÿ“ ์ด์ง„ ํƒ์ƒ‰

์ž…๊ตญ ์‹ฌ์‚ฌ

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
์ž…๊ตญ ์‹ฌ์‚ฌ

๋‚ด ๋‹ต์•ˆ

function solution(n, times) {
  times.sort((a, b) => a - b);

  let minTime = 0;
  let left = 1;
  let right = n * Math.max(...times);

  while (left <= right) {
    let midTime = Math.floor((left + right) / 2);
    let peopleCnt = 0;

    for (const time of times) {
      peopleCnt += Math.floor(midTime / time);
    }

    if (peopleCnt >= n) {
      minTime = midTime;
      right = midTime - 1;
    } else {
      left = midTime + 1;
    }
  }
  return minTime;
}

๐Ÿ’ก๊ตฌํ˜„ ๋ฐฉ๋ฒ•

์–ด์ œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠน๊ฐ•์—์„œ ์ œํ•œ์‚ฌํ•ญ์„ ๋ณด๋ฉด ๋Œ€์ถฉ ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์งœ์•ผํ•  ์ง€ ๋ณด์ธ๋‹ค๊ณ  ํ•˜์˜€๋‹ค.
๊ทธ๋ž˜์„œ ์ œํ•œ์‚ฌํ•ญ์— 1,000,000,000 ์ด๊ฐ€ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ๋Š” ๋ฌด์กฐ๊ฑด ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ํ‘ธ๋Š” ๊ฒƒ์ด๋ผ ํ™•์‹ ์„ ํ–ˆ๊ณ  ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

  1. ์šฐ์„  ์ด์ง„ ํƒ์ƒ‰์€ ๋ฐ์ดํ„ฐ๋“ค์ด ์ •๋ ฌ ๋˜์–ด ์žˆ์–ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋จผ์ € times๋ฅผ ์ •๋ ฌํ•˜์˜€๋‹ค.
  2. ์ตœ์†Œ, ์ตœ๋Œ€ ์‹œ๊ฐ„๋ฅผ ๊ตฌํ•œ ๋’ค ๊ตฌํ•˜๋ ค๋Š” ๋‹ต์„ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์ขํ˜€ ๊ฐ€๋ฉฐ ๋‹ต์„ ๊ตฌํ•œ๋‹ค.
    • ์ตœ์†Œ, ์ตœ๋Œ€ ์‹œ๊ฐ„์˜ ์ค‘๊ฐ„๊ฐ’์ธ midTime๊ฐ€ n๋ช…์„ ์‹ฌ์‚ฌ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒŒ์•…ํ•˜์˜€๋‹ค.
  3. n๋ช…์„ ์‹ฌ์‚ฌ ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด minTime์„ ๊ฐฑ์‹ ํ•˜๊ณ  ์ตœ๋Œ€ ๋ฒ”์œ„๋ฅผ ์ค„์ด๊ณ  n ๋ช…์„ ์‹ฌ์‚ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์ตœ์†Œ ์‹œ๊ฐ„์„ ๋Š˜๋ ค์ฃผ๋„๋ก ๋ฐ˜๋ณตํ•˜์˜€๋‹ค.

๋ชจ๋ฒ” ๋‹ต์•ˆ

function solution2(n, times) {
  const sortedTimes = times.sort((a, b) => a - b);
  let left = 1;
  let right = sortedTimes[sortedTimes.length - 1] * n;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    const sum = times.reduce((acc, time) => acc + Math.floor(mid / time), 0);

    if (sum < n) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }

  return left;
}

๐Ÿ”ฅ ๋Š๋‚€์ 

๋ฌธ์ œ ์ œํ•œ์กฐ๊ฑด์„ ๋ณด๊ณ  ์œ ํ˜• ์ฐพ๊ธฐโ—๏ธ
์–ด์ œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠน๊ฐ•์—์„œ ๋ฌธ์ œ ์œ ํ˜• ํŒŒ์•… ๋ฐฉ๋ฒ•์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ถ€๋ถ„์„ ์–˜๊ธฐํ•ด์ฃผ์…จ๋‹ค.

  1. ๋ฌธ์ œ ์œ ํ˜•์„ ๋ฐ”๋กœ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ขํ˜€๋‚˜๊ฐ€๋ผ
  2. ์ž…์ถœ๋ ฅ ์ œํ•œ๋ถ€ํ„ฐ ํ™•์ธ

๊ทธ๋ž˜์„œ ์˜ค๋Š˜ ๋ฌธ์ œ๋ฅผ ๋ณผ ๋•Œ 1,000,000,000 ๋ผ๋Š” ์ œํ•œ๊ณผ ~๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ€/์ตœ์†Œ ๊ฐ’์„ ์ฐพ์•„๋ผ์„ ๋ณด์ž๋งˆ์ž ๋ฐ”๋กœ ์ด์ง„ ํƒ์ƒ‰์ด ๋– ์˜ฌ๋ž๊ณ  "์•„ ์ด๋ ‡๊ฒŒ ์—ฐ์ƒ๋˜๋‹ˆ๊นŒ ๋ฐ”๋กœ ํ’€ ์ˆ˜ ์žˆ๊ฒ ๊ตฌ๋‚˜" ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.๐Ÿ˜Š

๋˜ํ•œ ์ €๋Š” minTime์„ ์„ ์–ธ ํ•˜์—ฌ ์ค‘๊ฐ„์— ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ left์™€ right๋งŒ์œผ๋กœ๋„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค. return๊ฐ’์„ left๋กœ ํ•˜๋ฉด ๋˜๋Š” ๊ฑฐ์˜€๋Š”๋ฐ.. ์ด๊ฒŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์™„๋ฒฝํžˆ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ด์„œ ์ƒ๊ธด ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹๊นŒ ์‹ถ์—ˆ๋‹ค.๐Ÿ˜ฑ
๋ช‡ ์ผ ๋™์•ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๊ณ  ์žˆ๋Š”๋ฐ ๋‹ค ์•Œ๊ณ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ—ˆ์ ๋„ ๋งŽ๊ณ  ํ—ˆ์ˆ ํ•˜๊ฒŒ ์ตํ˜”๋‹ค๋Š” ์ƒ๊ฐ์ด ๊ณ„์† ๋“ค์—ˆ๋‹ค. ์•ž์œผ๋กœ ์ง€๊ธˆ์ฒ˜๋Ÿผ ๊พธ์ค€ํžˆ ๋…ธ๋ ฅํ•ด์•ผ์ง€๐Ÿคฏ

Refer

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์˜ค๋Š˜์˜ ๋‚ด์šฉ ์ •๋ฆฌ

๋ฐ๋ธŒ์ฝ”์Šค Day5

profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 3์›” 25์ผ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠน๊ฐ•์—์„œ n ๋ฒ”์œ„ ๋ณด๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํƒํ•˜๋ผ๋Š” ๊ฑฐ ์ง„์งœ ์‹ค์ „ ๊ฟ€ํŒ์ธ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค..

1๊ฐœ์˜ ๋‹ต๊ธ€