์•Œ๊ณ ๋ฆฌ์ฆ˜) Two Pointer, Sliding Window

Two Pointer 2๊ฐœ์˜ ์ง€์ (ํฌ์ธํ„ฐ)์„ ์˜ฎ๊ฒจ๊ฐ€๋ฉฐ ๋‹ต์„ ๊ตฌํ•œ๋‹ค. ์ˆœ์ฐจ์  ์ ‘๊ทผ ๋“ฑ์ด ์š”๊ตฌ๋˜๋Š” ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. ex) ์ •๋ ฌ๋œ ๋‘ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฐฐ์—ด๋กœ ํ•ฉ์น  ๋•Œ(Merge Sort์—์„œ ์‚ฌ์šฉ๋จ), ์–ด๋–ค ๋ฐฐ์—ด์˜ ์—ฐ์† ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์›์†Œํ•ฉ์ด k์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š” ๊ฒฝ์šฐ ๋“ฑ ... ์ž์„ธํ•œ ๋‚ด์šฉ์€ Merge Sort ๋ถ€๋ถ„ ์ฐธ๊ณ  Sliding Window Two pointer์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๋‘ ํฌ์ธํ„ฐ ์‚ฌ์ด์˜ ๊ธธ์ด๊ฐ€ ๊ณ ์ •๋˜์–ด์žˆ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. Input Output ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ฐฐ์—ด D์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜ N๊ณผ ์œˆ๋„์šฐ ์‚ฌ์ด์ฆˆ K ๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๋ฐฐ์—ด์˜ ์›์†Œ Di๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. (Di ๋Š” ์ž์—ฐ์ˆ˜) ๋ฐฐ์—ด D์˜ ๊ธธ์ด๊ฐ€ K์ธ ์—ฐ์†๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์›์†Œ ํ•ฉ์˜ ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. import java.util...

Algorithm 2019. 9. 12. 19:04
์•Œ๊ณ ๋ฆฌ์ฆ˜) Sieve of of Eratosthenes (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด)

Sieve of of Eratosthenes (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด) ์†Œ์ˆ˜ํŒ๋ณ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•„์š”ํ•œ ๋ฒ”์œ„์˜ ๋ฐฐ์—ด ์ƒ์„ฑํ›„, 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์ˆ˜๋“ค์„ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ๊ฒƒ์œผ๋กœ ์ฒดํฌ ์ตœ์ ํ™” ์ฒซ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์˜ ๋ฒ”์œ„๋ฅผ 1-N์ด ์•„๋‹Œ 1-sqrt(N)์œผ๋กœ ๋ฐ”๊ฟ” ์†๋„ ํ–ฅ์ƒ ๊ฐ€๋Šฅ. A์˜ ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ• ๋•Œ 1-sqrt(A)๊นŒ์ง€๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๋Š”๊ฒƒ์€ ์ž๋ช…ํ•˜๋‹ค. ํ•ด๋‹น๋ฒ”์œ„๋‚ด์—์„œ N๋ฒˆ์งธ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, 1-N๊นŒ์ง€ ๋ชจ๋‘ ๋Œ๋ ค์•ผํ•˜๊ฒ ์ง€๋งŒ ํ•ด๋‹น๋ฒ”์œ„๋‚ด์—์„œ ํŠน์ •์ˆซ์ž N์ด ์†Œ์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, 1-sqrt(N)์œผ๋กœ ์ตœ์ ํ™” ๊ฐ€๋Šฅ. import java.lang.Math; import java.util.Scanner;; public class Test { private final static int MAX = 50; static boolea..

Algorithm 2019. 9. 12. 18:32
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ