Home πŸ„πŸ»β€β™€οΈ [μžλ°”μŠ€ν¬λ¦½νŠΈ] 이쀑 μš°μ„ μˆœμœ„ 큐
Post
Cancel

πŸ„πŸ»β€β™€οΈ [μžλ°”μŠ€ν¬λ¦½νŠΈ] 이쀑 μš°μ„ μˆœμœ„ 큐

πŸ“„ 문제

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μ΄μ€‘μš°μ„ μˆœμœ„ν

⚠️ λ°±μ€€ - κ³¨λ“œ IV 이쀑 μš°μ„ μˆœμœ„ 큐와 λ™μΌν•œ λ¬Έμ œλ‹€.

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 채점 ν™˜κ²½μ€ 비ꡐ적 λŠμŠ¨ν•˜κΈ°μ—(?) λ°°μ—΄μ˜ sort λ˜λŠ” shift, splice처럼 μ‹œκ°„ λ³΅μž‘λ„κ°€ 높은 λ©”μ„œλ“œλ₯Ό λ°˜λ³΅ν•΄μ„œ μ‚¬μš©ν•΄λ„ ν•΄κ²°λœλ‹€. λ°±μ€€μ—μ„œ μš°μ„ μˆœμœ„ 큐 클래슀둜 ν•΄κ²°ν•˜μ˜€κΈ°μ— μ—¬κΈ°μ„  λ°°μ—΄μ˜ λ©”μ„œλ“œλ§Œμ„ ν™œμš©ν•΄ 문제λ₯Ό ν•΄κ²°ν•˜μ˜€λ‹€.

κ΅¬ν˜„ν•΄μ•Ό ν•˜λŠ” 것은

λͺ…렁어에 따라 μ΅œλŒ“κ°’μ„ μ‚­μ œν•˜κ±°λ‚˜ μ΅œμ†Ÿκ°’μ„ μ‚­μ œν•΄μ•Ό ν•œλ‹€. ν•˜λ‚˜μ˜ 배열에 데이터가 μ‚½μž…λ  λ•Œλ§ˆλ‹€ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•΄μ€€λ‹€. μ‚­μ œ λͺ…령에 따라 μ΅œμ†Œκ°’ μ‚­μ œλŠ” κ°€μž₯ 첫 번째 μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³ , μ΅œλŒ€κ°’ μ‚­μ œλŠ” κ°€μž₯ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€.

μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν–ˆκΈ°μ— 배열에 남은 μ΅œμ†Œκ°’κ³Ό μ΅œλŒ€κ°’μ€ 각각 첫 λ²ˆμ§Έμ™€ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜λ©΄ λœλ‹€.

🏹 μ½”λ“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const solution = (operations) => {
  const heap = [];
  operations.forEach((operation) => {
    const [command, num] = operation.split(" ");
    console.log(operation);
    if (command === "I") {
      heap.push(+num);
      heap.sort((a, b) => a - b);
    } else if (+num === 1) {
      heap.pop();
    } else if (+num === -1) {
      heap.shift();
    }
  });

  return heap.length ? [Math.max(...heap), Math.min(...heap)] : [0, 0];
};
This post is licensed under CC BY 4.0 by the author.

πŸ„πŸ»β€β™€οΈ [μžλ°”μŠ€ν¬λ¦½νŠΈ] 성격 μœ ν˜• κ²€μ‚¬ν•˜κΈ°

πŸ“Έ 2022-11-04