λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/leetcode

LeetCode 1094 - Car Pooling (Medium)

by HandHand 2021. 3. 2.

문제

LeetCode - 1094번

풀이 κ³Όμ •

문제 λΆ„λ₯˜λŠ” νƒμš• μ•Œκ³ λ¦¬μ¦˜ μ΄μ§€λ§Œ μž…λ ₯ κ°’μ˜ λ²”μœ„κ°€ 크지 μ•ŠκΈ° λ•Œλ¬Έμ— 브루트 포슀 λ‘œλ„ μΆ©λΆ„νžˆ ν•΄κ²°ν•  수 μžˆλŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

μš°λ¦¬κ°€ μ°Ύμ•„μ•Ό ν•˜λŠ” 것은 ν•œ μ‹œμ μ— ν•„μš”ν•œ μ΅œλŒ€ capacity μž…λ‹ˆλ‹€.
λ”°λΌμ„œ μ‹œκ°„μ„ 0.5 λ‹¨μœ„λ‘œ μ¦κ°€μ‹œν‚€λ©° κ²ΉμΉ˜λŠ” μ΅œλŒ€ ꡬ간을 찾도둝 ν•©λ‹ˆλ‹€.

μ΄λ•Œ 0.5μ”© μ¦κ°€μ‹œμΌœμ€€ μ΄μœ λŠ” 거리가 1 λ‹¨μœ„μ΄κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
(저와 같은 λ°©λ²•μœΌλ‘œ κ΅¬ν˜„ν•œ λΆ„μ˜ μ½”λ“œλ₯Ό λ³΄λ‹ˆ μ‹œμž‘ 지점을 ν¬ν•¨ν•˜κ³  μ’…λ£Œ μ‹œκ°„μ„ μ œμ™Έν•˜μ—¬
1μ”© μ¦κ°€μ‹œμΌœλ„ λ˜λ„λ‘ κ΅¬ν˜„ν•œ 방법이 μžˆμ—ˆλŠ”λ° 그게 더 κ°„λ‹¨ν•΄λ³΄μž…λ‹ˆλ‹€.)

μ½”λ“œ

/**
 * @param {number[][]} trips
 * @param {number} capacity
 * @return {boolean}
 */

var carPooling = function (trips, capacity) {
  let maxValue = 0;
  for (let time = 0.0; time <= 1000; time += 0.5) {
    let need = 0;
    for (let [c, s, e] of trips) {
      if (s < time && time < e) {
        need += c;
      }
    }
    maxValue = Math.max(maxValue, need);
  }

  return maxValue <= capacity;
};

const trips = [
  [3, 2, 7],
  [3, 7, 9],
  [8, 3, 9],
];
const capacity = 11;
console.log(carPooling(trips, capacity));
λ°˜μ‘ν˜•

'πŸƒ algorithm > leetcode' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

LeetCode 283 - Move Zeroes (Medium)  (0) 2021.03.02
LeetCode 678 - Valid Parenthesis String (Medium)  (0) 2021.03.02
LeetCode 49 - Group Anagrams (Medium)  (0) 2021.03.02
LeetCode 200 - Number of Islands (Medium)  (0) 2021.03.02
LeetCode 64 - Minimum Path Sum (Medium)  (0) 2021.03.02

πŸ’¬ λŒ“κΈ€