Python/์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ฝ”ํ…Œ ์‹œ๊ฐ„๋ณต์žก๋„ ์ •๋ณตํ•˜๊ธฐ (๋น…์˜ค cheat sheet)

์ฃผ์˜ ๐Ÿฑ 2024. 8. 4. 21:58
728x90
๋ฐ˜์‘ํ˜•

https://github.com/jwasham/coding-interview-university?tab=readme-ov-file#algorithmic-complexity--big-o--asymptotic-analysis

 

GitHub - jwasham/coding-interview-university: A complete computer science study plan to become a software engineer.

A complete computer science study plan to become a software engineer. - jwasham/coding-interview-university

github.com

์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— ๊ด€ํ•œ ๊ฐœ์š”, ์ „๋ฐ˜์ ์œผ๋กœ ์ฐธ๊ณ ํ•˜๊ธฐ ์ข‹์€ ๊ธ€

๋น…์˜ค ํ‘œ๊ธฐ๋ฒ• cheat sheet ํ•œ์žฅ์— ์ •๋ฆฌ

 

 

 

 

๋น…์˜คํ‘œ๊ธฐ๋ฒ•์ด๋ž€?

์ตœ์•…์˜ ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ  ์ฝ”๋“œ ์‹คํ–‰์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ํ‘œ๊ธฐํ•œ ๊ฒƒ

์ฆ‰, ์ตœ์•…์ผ ๋•Œ(worst case)์˜ ์—ฐ์‚ฐ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ ํ‘œ๊ธฐ๋ฒ•

 

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ๋น…-์˜ค ํ‘œ๊ธฐ๋ฒ•(O(n))์„ ๊ธฐ์ค€์œผ๋กœ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ํ…Œ์ŠคํŠธ์—์„œ๋Š” 1๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ์„ ๊ฒฐ์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‘์‹œ์ž๊ฐ€ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•ด์•ผ๋งŒ ํ•ฉ๊ฒฉ์œผ๋กœ ํŒ๋‹จ ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ํŒ๋‹จํ•  ๋•Œ๋Š” ์ตœ์•…์ผ ๋•Œ worst case ๋ฅผ ์—ผ๋‘์— ๋‘ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๋น…-์˜ค ํ‘œ๊ธฐ๋ฒ•(O(n))์œผ๋กœ ํ‘œํ˜„ํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋ฐ์ดํ„ฐ ํฌ๊ธฐ(N)์˜ ์ฆ๊ฐ€์— ๋”ฐ๋ผ ์„ฑ๋Šฅ(์ˆ˜ํ–‰ ์‹œ๊ฐ„)์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๋ณต์žก๋„๋Š” ํ•ญ์ƒ ์ตœ์•…์ผ ๋•Œ, ์ฆ‰ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ํด ๋•Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

 

์—ฐ์‚ฐ ํšŸ์ˆ˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•

•์—ฐ์‚ฐ ํšŸ์ˆ˜ = ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ n๊ฐ’์— ๋ฐ์ดํ„ฐ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ ๋„์ถœ

 

์œ„ ๊ณต์‹์„ ๋Œ€์ž…ํ•ด ๊ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•œ์ง€ ํŒ๋‹จํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ ํ•ฉ์„ฑ ํ‰๊ฐ€

•๋ฒ„๋ธ” ์ •๋ ฌ = (1,000,000) 2 = 1,000,000,000,000 > 40,000,000 → ๋ถ€์ ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜

•๋ณ‘ํ•ฉ ์ •๋ ฌ = 1,000,000log 2 (1,000,000) = ์•ฝ 20,000,000 < 40,000,000 → ์ ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ์ œํ•œ์ด 2์ดˆ์ด๋ฏ€๋กœ ์—ฐ์‚ฐ ํšŸ์ˆ˜ 4,000๋งŒ ๋ฒˆ ์•ˆ์— ์›ํ•˜๋Š” ๋‹ต์„ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฒ„๋ธ” ์ •๋ ฌ์€ ์•ฝ 1์กฐ ๋ฒˆ์˜ ์—ฐ์‚ฐ ํšŸ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์— ์ ํ•ฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ์•ฝ 2,000๋งŒ ๋ฒˆ์˜ ์—ฐ์‚ฐ ํšŸ์ˆ˜๋กœ ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์— ์ ํ•ฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

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

 

์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ฝ”๋“œ ๋กœ์ง ๊ฐœ์„ ํ•˜๊ธฐ

์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ž‘์„ฑํ•œ ์ฝ”๋“œ์˜ ๋น„ํšจ์œจ์ ์ธ ๋กœ์ง logic ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ”ํƒ•์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ ๋‹ค. ์ด ๋ถ€๋ถ„์„ ํ™œ์šฉํ•˜๋ ค๋ฉด ๊ฐ€์žฅ ๋จผ์ € ์ฝ”๋“œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋„์ถœํ•˜๋ ค๋ฉด ๋‹ค์Œ 2๊ฐ€์ง€ ๊ธฐ์ค€์„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์‹œ๊ฐ„ ๋ณต์žก๋„ ๋„์ถœ ๊ธฐ์ค€

โ‘  ์ƒ์ˆ˜๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ณ„์‚ฐ์—์„œ ์ œ์™ธํ•œ๋‹ค.

โ‘ก ๊ฐ€์žฅ ๋งŽ์ด ์ค‘์ฒฉ๋œ ๋ฐ˜๋ณต๋ฌธ์˜ ์ˆ˜ํ–‰ ํšŸ์ˆ˜๊ฐ€ ์‹œ๊ฐ„ ๋ณต์žก๋„์˜ ๊ธฐ์ค€์ด ๋œ๋‹ค

 

ํ•ฉ ๋ฐฐ์—ด S ์ •์˜

S[i] = A[0] + A[1] + A[2] + … + A[i-1] + A[i] # A[0]๋ถ€ํ„ฐ A[i]๊นŒ์ง€์˜ ํ•ฉ

ํ•ฉ ๋ฐฐ์—ด์€ ๊ธฐ์กด์˜ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•œ ๋ฐฐ์—ด์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ฉ ๋ฐฐ์—ด์„ ๋ฏธ๋ฆฌ ๊ตฌํ•ด ๋†“์œผ๋ฉด ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ์˜ ์ผ์ • ๋ฒ”์œ„์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(N)์—์„œ O(1)๋กœ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ํ•ฉ ๋ฐฐ์—ด์„ ์ข€ ๋” ์ž์„ธํžˆ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ธ๋ฑ์Šค

๋ฆฌ์ŠคํŠธ A 

ํ•ฉ ๋ฐฐ์—ด S

0 1 2 3 4 5

15 13 10 7 3 12

15 28 38 45 48 60

 

 

ํ•ฉ ๋ฐฐ์—ด

A[i]๋ถ€ํ„ฐ A[j]๊นŒ์ง€์˜ ๋ฆฌ์ŠคํŠธ ํ•ฉ์„ ํ•ฉ ๋ฐฐ์—ด ์—†์ด ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ, ์ตœ์•…์˜ ๊ฒฝ์šฐ๋Š” i๊ฐ€ 0์ด๊ณ  j๊ฐ€ N์ธ ๊ฒฝ์šฐ๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์•ž์—์„œ ์•Œ์•„๋ณธ ํ•ฉ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด O(1) ์•ˆ์— ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ ๋ฐฐ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ๊ณต์‹์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•ฉ ๋ฐฐ์—ด S๋ฅผ ๋งŒ๋“œ๋Š” ๊ณต์‹

S[i] = S[i-1] + A[i]

 

 

์ถœ์ฒ˜

 

https://www.bigocheatsheet.com/

 

Big-O Algorithm Complexity Cheat Sheet (Know Thy Complexities!) @ericdrowell

Know Thy Complexities! Hi there!  This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.  When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting t

www.bigocheatsheet.com

 

์‹œ๊ฐ„๋ณต์žก๋„ ํ•œ๋ฒˆ์— ์ •๋ฆฌํ•ด๋‘” ์‚ฌ์ดํŠธ

 

 

๋ฐ˜์‘ํ˜•