์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

(Python) ํŒŒ์ด์ฌ - list, Dict์˜ ๋ฉ”์†Œ๋“œ ์‹œ๊ฐ„๋ณต์žก๋„

  • -

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋“ค์„ ํ’€๋‹ค๋ณด๋ฉด ๋กœ์ง๊ณผ ๋„์ถœ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์€ ๊ฐ™์ง€๋งŒ, ์‹œ๊ฐ„๋ณต์žก๋„ ๋•Œ๋ฌธ์— ์• ๋จน๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค.

ํ™•์‹คํžˆ ์ž…๋ ฅ๊ฐ’๋“ค์ด ๋งŽ์œผ๋ฉด ๋งŽ์„์ˆ˜๋ก ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ณ ๋ คํ•ด์•ผํ• ๊ฒƒ๊ฐ™๋‹ค.

https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

 

 

์ด๊ณณ์— ๊ฐ€๋ฉด ์˜จ๊ฐ– ์—ฐ์‚ฐ๋“ค์— ๋Œ€ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์žˆ๋‹ค. ์ž์ฃผ ์“ฐ๋Š”๊ฒƒ๋“ค๋งŒ ์ •๋ฆฌํ•ด๋ณด๋ฉด

 

Python์˜ List ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„

Operation ex BigO Notes
Index arr[i] O(1) ๊ทธ๋ƒฅ ๊ฐ’์— ์ ‘๊ทผ
Length len(arr) O(1)  
Append arr.append(4) O(1)  
Pop arr.pop() O(1)  
Slice arr[a:b] O(b-a) ์ž๋ฅผ ๊ธธ์ด์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค
Extend arr.extend(...) O(len(...)) ํ™•์žฅํ•  ๊ธธ์ด์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.
Insert arr.insert(i, v) O(N)  
Delete del arr[i] O(N)  
Pop arr.pop(i) O(N)  
Reverse Arr.reverse() O(N)  
Sort arr.sort() O(N LogN)  
Iteration for i in arr: O(N)  
Store arr[i] = v O(1) ๊ทธ๋ƒฅ ์ €์žฅ, ์ˆ˜์ •ํ•˜๋Š”๊ฒƒ์„ ๋งํ•จ

์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜๋Š” ์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ์— ๋ฌด์กฐ๊ฑด O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค. (๊ตต์€ ๊ธ€์”จ) ์ด ์ž๋ฃŒ๋“ค์„ ๋ณด๋‹ˆ ์™œ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค Stack์ด๋‚˜, Queue๋“ฑ์ด ์—ฐ์‚ฐ์ด ๋น ๋ฅธ์ง€ ์•Œ๊ฒƒ๊ฐ™๋‹ค.

Python Dictionary ์—ฐ์‚ฐ์˜ ์‹œ๊ฐ„๋ณต์žก๋„

Operation ex BigO Notes
Index d[k] O(1) ๊ทธ๋ƒฅ ๊ฐ’์— ์ ‘๊ทผ
Store d[k] = v O(1) ๊ทธ๋ƒฅ ์ €์žฅ
Length len(d) O(1)  
Delete del d[k] O(1)  
Get d.get O(1)  
Pop d.pop(k) O(1)  
Pop item d.popitem() O(1)  
Veiw d.keys() / d.values() O(1)  

ํ™•์‹คํžˆ Key์™€ Value๊ฐ’์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฐ์˜ O(1)์ด๋‹ค.

set์€ ์‚ฌ์šฉํ• ์ผ์ด ์ž˜ ์—†์–ด์„œ ์ƒ๋žต..

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.