user-img
์ค‘ํ•™์ƒ๋„ ์•Œ์•„๋“ฃ๊ฒŒ 1
thumbnail
์ด๋ถ„ ํƒ์ƒ‰ & Upper Bound, Lower Bound ๊ฐœ๋… ์ •๋ฆฌ
1. Binary-Search๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?์ •๋ ฌ๋œ ๋ฐฐ์—ด ํ˜น์€ List ์—์„œ ํƒ์ƒ‰ ๊ตฌ๊ฐ„์„ ์ ˆ๋ฐ˜์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ํŠน์ • ๊ฐ’์„ ์ฐพ๋Š” ํƒ์ƒ‰๋ฒ•์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ ๋œ๋‹ค.(0) ๊ธฐ๋ณธ์ ์ธ ์šฉ์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.target: ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’, start : ์™ผ์ชฝ ํฌ์ธํ„ฐ,(index=0์—์„œ ์‹œ์ž‘), end: ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ(๋ฐฐ์—ด ๋์—์„œ ์‹œ์ž‘), mid: ๋‘ ๊ฐ’์˜ ์ค‘๊ฐ„์— ์œ„์น˜ํ•œ ๊ฐ’(1) mid๋ฅผ ๊ตฌํ•˜๊ณ  target ๊ฐ’๊ณผ ๋Œ€์†Œ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•œ๋‹ค.(2) mid > target ์ด๋ฉด, mid ๊ฐ’์ด target๊ฐ’์œผ๋กœ ํ–ฅํ•˜๋„๋ก, end = mid-1 ๋กœ ํƒ์ƒ‰ ์˜์—ญ์„ ์ ˆ๋ฐ˜ ์ค„์ธ๋‹ค.(3) mid target ์ด๋ฉด, start = mid +1 ์ด ๋˜๋„๋ก ํ•˜์—ฌ, ํƒ์ƒ‰ ์˜์—ญ์„ ์ ˆ๋ฐ˜ ์ค„์ธ๋‹ค.(4) mid == target ์ด๋ฉด ๊ฐ’์„ ๊ตฌํ–ˆ์œผ๋ฏ€๋กœ, ์ด๋ถ„ ..
2024.07.24
์•Œ๊ณ ๋ฆฌ์ฆ˜/์•Œ๊ณ ๋ฆฌ์ฆ˜-์ด๋ก