Algorithms in plain English: time complexity and Big O notation - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Algorithms in plain English: time complexity and Big-O notation

๋ฒˆ์—ญ : https://www.freecodecamp.org/news/time-is-complex-but-priceless-f0abd015063c/

๋ชจ๋“  ํ›Œ๋ฅญํ•œ ๊ฐœ๋ฐœ์ž๋Š” ์‹œ๊ฐ„์— ์ค‘์š”์‹œ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋” ๋งŽ์€ ๊ฒƒ์„ ์ œ๊ณตํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž์˜ ์ฆ๊ฑฐ์›€์— ๊ด€ํ•ด์„œ ๋ฌด์—‡์ด๋“  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก์„ฑ์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋‹น์‹ ์ด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ „์— ์–ด๋””์„œ ๋Œ€๊ฒŒ ์ ์šฉ์ด ๋˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. : ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋””์ž์ธ ์—์„œ ์ ์šฉ์ด ๋œ๋‹ค.

So whatโ€™s an algorithm, anyway?

๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฌด์—‡์ด๋ƒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ ˆ์ฐจ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋‹จ๊ณ„์ด๋‹ค. ์ด๊ฒƒ์€ ํ•œ ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑ ๋˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ ์œ„ํ•œ ๋‹จ๊ณ„ ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ํ• ๋จธ๋‹ˆ์˜ ์ผ€์žŒ์„ ๋งŒ๋“œ๋Š” ๋ ˆ์‹œํ”ผ๋ฅผ ์‚ดํŽด ๋ด…์‹œ๋‹ค. ์ž ๊น, ์—ฌ๊ธฐ์„œ ๋ช‡๊ฐœ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž.

function BakeCake(flavor, icing){
"
 1. Heat Oven to 350 F
 2. Mix flour, baking powder, salt
 3. Beat butter and sugar until fluffy
 4. Add eggs.
 5. Mix in flour, baking powder, salt
 6. Add milk and " + flavor + "
 7. Mix further
 8. Put in pan
 9. Bake for 30 minutes
10." + if(icing === true) return 'add icing' + "
10. Stuff your face
"
}

BakeCake('vanilla', true) => deliciousness

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์‹œํ—˜ํ•˜๊ธฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์€ ๋ชจ๋“  ๋ชจ์–‘๊ณผ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ผ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํŒŒ์ด๋ฅผ ์กฐ๊ฐ๋‚ผ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด 100๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํ–ˆ์„๋•Œ, ๋‹น์‹ ์€ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ํ•œ๊ฐ€์ง€๋กœ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช‡๋ช‡ ํ•ด๊ฒฐ์ฑ…๋“ค์€ ํšจ์œจ์ ์ด๊ณ  ์‹œ๊ฐ„์ด ๋œ ๋“ค๋ฉด์„œ ๋‹ค๋ฅธ๊ฒƒ๋ณด๋‹ค ๋” ์ ์€ ๊ณต๊ฐ„์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ฃผ๋œ ์งˆ๋ฌธ์€ ์–ด๋–ป๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ํ•ด๊ฒฐ์ฑ…์ด ๊ฐ€์žฅ ํšจ์œจ์ ์ธ์ง€ ๋ถ„์„์„ ํ•˜๋Š”๊ฐ€์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค.

์ˆ˜ํ•™์ด ํ•ด๊ฒฐํ•ด์ค„๊ฒƒ์ด๋‹ค! ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„์€ ์ฃผ์–ด์ง„ ์ž…๋ ฅ ์ˆ˜ (n)๋ฅผ ๊ฐ€์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๋ถ„์„ํ•˜๋Š” ๋งค์šฐ ๋‹จ์ˆœํ™” ๋œ ์ˆ˜ํ•™์  ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋Œ€๊ฒŒ Big-O ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ •์˜ํ•œ๋‹ค.

Whatโ€™s Big O notation, you ask?

Big-O ํ‘œ๊ธฐ๋ฒ•์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ „์ฒด ๋‹จ๊ณ„๋ฅผ ๋Œ€์ˆ˜์šฉ์–ด๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ ํ›„์— ๋ฌธ์ œ์˜ ์ „์ฒด ๋ณต์žก์„ฑ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š” ํ•˜์œ„ ์ƒ์ˆ˜ ๋ฐ ๊ณ„์ˆ˜๋ฅผ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜ํ•™์ž๋“ค์€ ์•„๋งˆ๋„ ๋‚ด "์ „๋ฐ˜์ ์ธ ์˜ํ–ฅ" ๊ฐ€์ •์— ๋Œ€ํ•ด ์•ฝ๊ฐ„์˜ ๋น„๋‚œ์„ ํ•  ๊ฒƒ์ด์ง€๋งŒ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ผ์„ ๋‹จ์ˆœํ™”ํ•˜๋Š” ๊ฒƒ์ด ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค.

Regular       Big-O

2             O(1)   --> It's just a constant number

2n + 10       O(n)   --> n has the largest effect

5n^2          O(n^2) --> n^2 has the largest effect

๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ,์ด ๋ชจ๋“  ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ‘œํ˜„์ด ๋ฐ˜ํ™˜ ํ•  ๊ฐ€์น˜์— ๊ฐ€์žฅ ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜์žˆ๋Š” ํ‘œํ˜„์˜ ์š”์†Œ๋งŒ์„ ๋ด…๋‹ˆ๋‹ค. (์ƒ์ˆ˜๊ฐ€ ํด์ˆ˜๋ก n์ด ์ž‘์•„ ์ง€์ง€๋งŒ, ์ง€๊ธˆ์€ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„๋ฉ๋‹ˆ๋‹ค.)

๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์ •์˜๋กœ ์ธํ•œ ์ผ๋ฐ˜์ ์ธ ์‹œ๊ฐ„ ๋ณต์žก๋„์ž…๋‹ˆ๋‹ค. ๋ณด๋‹ค ์ž์„ธํ•œ ์ •์˜๋Š” Wikipedia๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

  • O (1) โ€” ์ƒ์ˆ˜ ์‹œ๊ฐ„ : ํฌ๊ธฐ n์˜ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๋‹จ ํ•œ ๋‹จ๊ณ„ ๋งŒ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.
  • O (log n) โ€” ๋กœ๊ทธ ์‹œ๊ฐ„ : ํฌ๊ธฐ n์˜ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๊ฐ€ ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ์ผ๋ถ€ ์š”์ธ์œผ๋กœ ์ค„์–ด ๋“ญ๋‹ˆ๋‹ค.
  • O (n) โ€” ์„ ํ˜• ์‹œ๊ฐ„ : ํฌ๊ธฐ n์˜ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ํ•„์š”ํ•œ ๋‹จ๊ณ„ ์ˆ˜๋Š” ์ง์ ‘ ๊ด€๋ จ๋ฉ๋‹ˆ๋‹ค (1-1).
  • O (nยฒ) โ€” 2 ์ฐจ ์‹œ๊ฐ„ : ํฌ๊ธฐ n์˜ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋Š” n์˜ ์ œ๊ณฑ์ž…๋‹ˆ๋‹ค.
  • O (C ^ n) โ€” ์ง€์ˆ˜ ์‹œ๊ฐ„ : ํฌ๊ธฐ n์˜ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„์˜ ์ˆ˜๋Š” n์˜ ์ œ๊ณฑ (์ƒ๋‹นํžˆ ํฐ ์ˆ˜)์ž…๋‹ˆ๋‹ค.

์ด ์ง€์‹์„ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋Ÿฌํ•œ ๊ฐ ๋ณต์žก์„ฑ์— ์ˆ˜๋ฐ˜๋˜๋Š” ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

let n = 16;

O (1) = 1 step "(awesome!)"

O (log n) = 4 steps  "(awesome!)" -- assumed base 2

O (n) = 16 steps "(pretty good!)"

O(n^2) = 256 steps "(uhh..we can work with this?)"

O(2^n) = 65,536 steps "(...)"

๋ณด์‹œ๋‹ค์‹œํ”ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ณต์žก์„ฑ์— ๋”ฐ๋ผ ์ƒํ™ฉ์ด ํ›จ์”ฌ ๋ณต์žกํ•ด์ง‘๋‹ˆ๋‹ค. ์šด ์ข‹๊ฒŒ๋„ ์ปดํ“จํ„ฐ๋Š” ์—ฌ์ „ํžˆ ๋งค์šฐ ํฐ ๋ณต์žก์„ฑ์„ ๋น„๊ต์  ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜์žˆ์„๋งŒํผ ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด Big-O ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋‹ค์Œ์€ ์ด๋Ÿฌํ•œ ์ง€์‹์„ ์•ผ์ƒ์—์„œ ์ ‘ํ•˜๊ฑฐ๋‚˜ ์ง์ ‘ ์ฝ”๋”ฉ ํ•  ์ˆ˜์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค.

์•„๋ž˜์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

var friends = {
 'Mark' : true,
 'Amy' : true,
 'Carl' : false,
 'Ray' :  true,
'Laura' : false,
}
var sortedAges = [22, 24, 27, 29, 31]

O(1) โ€” Constant Time

ํ‚ค (๊ฐ์ฒด) ๋˜๋Š” ์ธ๋ฑ์Šค (๋ฐฐ์—ด)๊ฐ€ ํ•ญ์ƒ ํ•œ ๋‹จ๊ณ„ ์”ฉ ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์ผ์ •ํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฉด ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค.

//If I know the persons name, I only have to take one step to check:

function isFriend(name){ //similar to knowing the index in an Array 
  return friends[name]; 
};

isFriend('Mark') // returns True and only took one step

function add(num1,num2){ // I have two numbers, takes one step to return the value
 return num1 + num2
}

O(log n) โ€” Logarithmic Time

๋ฐฐ์—ด์˜ ์–ด๋Š ์ชฝ์—์„œ ํ•ญ๋ชฉ์„ ์ฐพ์•„์•ผํ•˜๋Š”์ง€ ์•Œ๋ฉด ๋‚˜๋จธ์ง€ ์ ˆ๋ฐ˜์„ ์ž˜๋ผ์„œ ์‹œ๊ฐ„์„ ์ ˆ์•ฝ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

//You decrease the amount of work you have to do with each step

function thisOld(num, array){
  var midPoint = Math.floor( array.length /2 );
  if( array[midPoint] === num) return true;
  if( array[midPoint] < num ) --> only look at second half of the array
  if( array[midpoint] > num ) --> only look at first half of the array
  //recursively repeat until you arrive at your solution
  
}

thisOld(29, sortedAges) // returns true 

//Notes
 //There are a bunch of other checks that should go into this example for it to be truly functional, but not necessary for this explanation.
 //This solution works because our Array is sorted
 //Recursive solutions are often logarithmic
 //We'll get into recursion in another post!

O(n) โ€” Linear Time

์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋ฐฐ์—ด ๋˜๋Š” ๋ชฉ๋ก์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์‚ดํŽด ๋ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ for ๋ฃจํ”„๋Š” ๊ฑฐ์˜ ํ•ญ์ƒ ์„ ํ˜• ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ indexOf์™€ ๊ฐ™์€ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋„ ์„ ํ˜• ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ๋ฃจํ•‘ ๊ณผ์ •์—์„œ ์ถ”์ƒํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

//The number of steps you take is directly correlated to the your input size

function addAges(array){
  var sum = 0;
  for (let i=0 ; i < array.length; i++){  //has to go through each value
    sum += array[i]
  }
 return sum;
}

addAges(sortedAges) //133

O(nยฒ) โ€” Quadratic Time

์ค‘์ฒฉ ๋œ for ๋ฃจํ”„๋Š” ๋‹ค๋ฅธ ์„ ํ˜• ์—ฐ์‚ฐ (๋˜๋Š” n * n = nยฒ) ๋‚ด์—์„œ ์„ ํ˜• ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 2 ์ฐจ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.

//The number of steps you take is your input size squared

function addedAges(array){
  var addedAge = [];
    for (let i=0 ; i < array.length; i++){ //has to go through each value
      for(let j=i+1 ; j < array.length ; j++){ //and go through them again
        addedAge.push(array[i] + array[j]);
      }
    }
  return addedAge;
}

addedAges(sortedAges); //[ 46, 49, 51, 53, 51, 53, 55, 56, 58, 60 ]

//Notes
 //Nested for loops. If one for loop is linear time (n)
 //Then two nested for loops are (n * n) or (n^2) Quadratic!

O(2^n) โ€” Exponential Time

์ง€์ˆ˜ ์‹œ๊ฐ„์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ทธ ์ •๋„๋ฅผ ๋ชจ๋ฅด๊ณ  ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ ๋˜๋Š” ์ˆœ์—ด์„ ์‹œ๋„ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์„์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

//The number of steps it takes to accomplish a task is a constant to the n power

//Thought example
 //Trying to find every combination of letters for a password of length n

๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋˜์–ด์•ผํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋งˆ๋‹ค ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๊ฒฝ๋กœ๊ฐ€์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋จผ์ € ์ž‘๋™ํ•˜๋Š” ์†”๋ฃจ์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ˜„๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์žฅ๊ธฐ์ ์œผ๋กœ๋Š” ๊ฐ€๋Šฅํ•œ ํ•œ ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์†”๋ฃจ์…˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์— ๋„์›€์ด๋˜๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•œ ์งˆ๋ฌธ์„ํ•ฉ๋‹ˆ๋‹ค.

  1. ์ด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ๋Š”๊ฐ€? ์˜ˆ =>
  2. ์ด ์ž‘์—…์— ์‹œ๊ฐ„์„ ๊ฐ–๋Š”๊ฐ€? ์˜ˆ => 3๋ฒˆ์œผ๋กœ, ์•„๋‹ˆ์˜ค => ๋‚˜์ค‘์— ๋‹ค์‹œ ๋Œ์•„์™€์„œ 6 ๋‹จ๊ณ„๋กœ ๊ฐ€์‹ญ์‹œ์˜ค.
  3. ๋ชจ๋“  ์—ฃ์ง€ ์ผ€์ด์Šค์— ์ปค๋ฒ„ํ–ˆ์Šต๋‹ˆ๊นŒ? ์˜ˆ =>
  4. ๋‚ด ๋ณต์žก์„ฑ์ด ๊ฐ€๋Šฅํ•œ ํ•œ ๋‚ฎ์Šต๋‹ˆ๊นŒ? ์•„๋‹ˆ์˜ค => ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ• ๋˜๋Š” ์ˆ˜์ •์œผ๋กœ ์žฌ ์ž‘์„ฑํ•˜์‹ญ์‹œ์š” -> 1๋ฒˆ ๋‹จ๊ณ„๋กœ ๋‹ค์‹œ ๊ฐ€์‹ญ์‹œ์˜ค. , ์˜ˆ => 5๋ฒˆ์œผ๋กœ ๊ฐ€์‹ญ์‹œ์˜ค.
  5. ์ฝ”๋“œ๊ฐ€ D.R.Y ํ•ฉ๋‹ˆ๊นŒ? ์˜ˆ =>
  6. ๋งŒ์กฑํ•œ๊ฐ€! ์•„๋‹ˆ์˜ค => ๋‹ค์‹œ D.R.Y๋กœ ์งœ๊ณ ๋‚˜์„œ ๋งŒ์กฑํ•ด๋ผ.

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ๋งˆ๋‹ค ์‹œ๊ฐ„ ๋ณต์žก์„ฑ์„ ๋ถ„์„ํ•˜์‹ญ์‹œ์˜ค. ๋กœ๊ทธ ์‹คํ–‰์—์„œ ๋” ๋‚˜์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒ€์›๊ณผ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹น์‹ ์„ ์ข‹์•„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋“  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์ด๋“  ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง๋ฉดํ•˜๊ฒŒ ๋  ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ œ๋Š” ์ˆ˜๋ฐฑ ๊ฐ€์ง€์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ์ˆ˜์‹ญ ๊ฐ€์ง€๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ• ์ง€ ๋˜๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ€ ํ”„๋กœ์ ํŠธ์— Angular, React ๋˜๋Š” Backbone์„ ์‚ฌ์šฉํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์†”๋ฃจ์…˜์€ ๋ชจ๋‘ ๋™์ผํ•œ ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ๊ฐ์•ˆํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์— ๋Œ€ํ•œโ€œ์˜ฌ๋ฐ”๋ฅธโ€๋˜๋Š”โ€œ์ตœ์ƒ์˜โ€๋‹ต๋ณ€์ด ์žˆ๋‹ค๊ณ  ๋งํ•˜๊ธฐ๋Š” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ฃผ์–ด์ง„ ๋ฌธ์ œ์— ๋Œ€ํ•ดโ€œ๋” ๋‚˜์€โ€๋˜๋Š”โ€œ๋” ๋‚˜์œโ€๋‹ต๋ณ€์ด ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ „ ์˜ˆ์ œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒ€์˜ ์ ˆ๋ฐ˜์ด ๊ฒฝํ—˜์ด์žˆ๋Š” ํŒ€ ํ”„๋กœ์ ํŠธ์— React๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์œผ๋ฏ€๋กœ ์‹œ์ž‘ํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋œ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

๋” ๋‚˜์€ ์†”๋ฃจ์…˜์„ ์„ค๋ช…ํ•˜๋Š” ๋Šฅ๋ ฅ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‹œ๊ฐ„ ๋ณต์žก์„ฑ ๋ถ„์„๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์ž˜ ํ•ด๊ฒฐํ•˜์‹ญ์‹œ์˜ค. Big-O๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฉ๋ฒ•์„ ํŒŒ์•…ํ•˜์‹ญ์‹œ์˜ค.

์ตœ์ข… ์š”์•ฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • O (1) โ€” ์ƒ์ˆ˜ ์‹œ๊ฐ„ : ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๋‹จ ํ•œ ๋‹จ๊ณ„ ๋งŒ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.
  • O (log n) โ€” ๋กœ๊ทธ ์‹œ๊ฐ„ : ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋Š” ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ์ผ๋ถ€ ์š”์ธ์— ์˜ํ•ด ์ค„์–ด ๋“ญ๋‹ˆ๋‹ค.
  • O (n) โ€” ์„ ํ˜• ์‹œ๊ฐ„ : ํ•„์š”ํ•œ ๋‹จ๊ณ„ ์ˆ˜๋Š” ์ง์ ‘ ๊ด€๋ จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค (1-1).
  • O (nยฒ) โ€” 2 ์ฐจ ์‹œ๊ฐ„ : ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋Š” n์˜ ์ œ๊ณฑ์ž…๋‹ˆ๋‹ค.
  • O (C ^ n) โ€” ์ง€์ˆ˜ : ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋Š” n ์ œ๊ณฑ (์ƒ์ˆ˜)์— ๋Œ€ํ•œ ์ƒ์ˆ˜์ž…๋‹ˆ๋‹ค.
โš ๏ธ **GitHub.com Fallback** โš ๏ธ