ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๐Ÿ’ก Two Pointer
๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋กํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

๋ฌธ์ œ

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋‘ ๋ฐฐ์—ด์„ ํ•ฉ์ณ ์ƒˆ๋กœ์šด ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ ์˜ˆ์ œ

2 4 5 6
1 2 6 8 10

 

์ถœ๋ ฅ ์˜ˆ์ œ

1 2 2 4 5 6 6 8 10

 

 

 

 

ํ’€์ด

๋‘ ๊ฐœ์˜ ๊ฐ ๋ฐฐ์—ด์— ํฌ์ธํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋‘๊ณ  ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ž‘์€ ๊ฐ’์„ answer์— ๋‹ด๋Š”๋‹ค.

 

function solution(a, b) {
  let answer = [];
  let n = a.length;
  let m = b.length;
  let p1 = p2 = 0;

  // ๋‘ ๋ฐฐ์—ด ์ค‘ ํ•œ ๋ฐฐ์—ด์˜ ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด ๋ฐ˜๋ณต ์ข…๋ฃŒ
  while (p1 < n && p2 < m) {
    if (a[p1] <= b[p2]) answer.push(a[p1++]); // a[p1]์„ pushํ•˜๊ณ  p1++
    else answer.push(b[p2++]);
  }
  // p1์ด ์•„์ง ๋‚จ์€ ๊ฒฝ์šฐ
  while (p1<n) answer.push(a[p1++]);
  // p2๊ฐ€ ์•„์ง ๋‚จ์€ ๊ฒฝ์šฐ
  while (p2<m) answer.push(b[p2++]);

  return answer;
}
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31