๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ํˆฌ ํฌ์ธํ„ฐ & ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ

๋ฐ˜์‘ํ˜•

ํˆฌ ํฌ์ธํ„ฐ & ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

1์ฐจ์› ๋ฐฐ์—ด์„ 2ํšŒ ์ด์ƒ ๋ฐ˜๋ณต์ ์œผ๋กœ ํƒ์ƒ‰์„ ํ•ด์•ผํ•  ๊ฒฝ์šฐ ๋‹จ์ˆœํ•˜๊ฒŒ ํ•œ๋‹ค๋ฉด O(N^2) ์ด์ƒ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฌ์ง€๋งŒ ํˆฌ ํฌ์ธํ„ฐ์™€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ O(N)์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๊ณตํ†ต์ ์ด ์žˆ๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ๋Š” ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋Š” ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ณ ์ •์ ์ด๋ผ๋Š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ(Two Pointer)

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 1์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” 2๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์กฐ์ž‘ํ•ด๊ฐ€๋ฉด์„œ ์›ํ•˜๋Š” ๊ฒƒ์„ ์–ป๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

์ด๋Ÿฌํ•œ ํŠน์„ฑ ๋•Œ๋ฌธ์— ํˆฌ ํฌ์ธํ„ฐ๋Š” ์—ฐ์†๋˜๊ณ  ๊ธธ์ด๊ฐ€ ๊ฐ€๋ณ€์ ์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ผ์น˜์‹œํ‚ค๋Š” ๋ฌธ์ œ์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์—์„œ๋Š” ๋ชจ๋“  ๋ฐฐ์—ด๋“ค์˜ ๊ฐ’๋“ค์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐœ์ˆ˜, ํ˜น์€ ์ตœ์ €, ์ตœ๋Œ€ ๊ฐ’ ๋“ฑ์„ ์ฐพ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ‘์กฐํ•ฉ', ‘๋ฐฑํŠธ๋ž˜ํ‚น'์œผ๋กœ ํ‘ผ๋‹ค๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋ฒ—์–ด๋‚  ์ˆ˜ ์—†๋‹ค.

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋ณดํ†ต (start, end) ๋˜๋Š” (left, right)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์ด ์กด์žฌํ•œ๋‹ค.

  1. 2๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ์‹œ์ž‘์ ์ด ๋ฐฐ์—ด์˜ ์‹œ์ž‘์ ์ธ ๊ฒฝ์šฐ.
  2. ์ •๋ ฌ๋œ ๋ฐฐ์—ด ์•ˆ์—์„œ 2๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ๊ฐ ์‹œ์ž‘์ ๊ณผ ๋์ (arr.length-1)์— ์œ„์น˜ํ•œ ๊ฒฝ์šฐ

์ฒซ๋ฒˆ์งธ ์œ ํ˜•์œผ๋กœ ํ•ฉ์ด T์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ฐพ๋Š”๋‹ค๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. start = end = 0, sum = arr[0]์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
  2. while(start≤end) ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋งŒ๋“ ๋‹ค.
  3. sum๊ณผ T๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.3.2. sum > T๋ผ๋ฉด, sum-= arr[start] ํ•ด์ฃผ๊ณ , start+1์„ ํ•ด์ค˜์„œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ฐ์†Œ์‹œ์ผœ์„œ ํ•ฉ์„ ๊ฐ์†Œ์‹œ์ผœ์ค€๋‹ค.
  4. 3.3. sum == T ์ด๋ฉด ๋!
  5. 3.1. sum < T ๋ผ๋ฉด, end๋ฅผ +1 ํ•ด์ค˜์„œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ฆ๊ฐ€ ์‹œ์ผœ์ฃผ๊ณ  sum += arr[end]๋กœ ํ•ฉ์„ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

์ด ๋•Œ start์™€ end๊ฐ€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ํ•˜๋‚˜์”ฉ ๋” ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ(Sliding Window)

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํˆฌ ํฌ์ธํ„ฐ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ ์ •ํ•ด์„œ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ํˆฌ ํฌ์ธํ„ฐ์™€ ๋‹ฌ๋ฆฌ ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”์—†์ด ๊ณ ์ •์ ์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์™€ ํ•œ ๊ฐœ์˜ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์–‘ ๋์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ๊ณ ์ • ๊ธธ์ด l๊ณผ ์‹œ์ž‘ ์ง€์  ํฌ์ธํ„ฐ p = 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  2. p๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ (๋ฐฐ์—ด ๊ธธ์ด - l - 1 )๊นŒ์ง€ ํƒ์ƒ‰ํ•œ๋‹ค.

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋Š” ๊ธธ์ด๊ฐ€ ๊ณ ์ •์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•