๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Code Tree

[์ฝ”๋“œํŠธ๋ฆฌ] ๊ณต๊ฐ„๋ณต์žก๋„

728x90
SW์ค‘์‹ฌ๋Œ€ํ•™ ์‚ฌ์—…๋‹จ์—์„œ CodeTree์™€ ํ•จ๊ป˜ ์‹ค์‹œํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„ ์บ ํ”„์— ์ฐธ์—ฌํ•˜์—ฌ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

ํ”„๋กœ๊ทธ๋žจ์˜ ์†Œ์š”์‹œ๊ฐ„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ปดํ“จํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•œ์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๊ณต๊ฐ„๋ณต์žก๋„๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

๊ณต๊ฐ„๋ณต์žก๋„๋„ ์‹œ๊ฐ„๋ณต์žก๋„์™€ ๋™์ผํ•˜๊ฒŒ ์ ๊ทผ์  ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

function example(n)
  set arr = [n][n]
  for i = 0 ... i < n
    for j = 0 ... j < n
      arr[i][j] = 1
 

๊ณต๊ฐ„๋ณต์žก๋„ = '์ด ์ฝ”๋“œ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์–ผ๋งˆ๋‚˜ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์„๊นŒ?'

์œ„ ์ฝ”๋“œ์—์„œ set arr = [n][n] ์ด์™ธ์˜ ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—,

๊ณต๊ฐ„๋ณต์žก๋„๋Š”  ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ๊ณต๊ฐ„๋ณต์žก๋„๋Š” ์™œ ํ•„์š”ํ• ๊นŒ์š”?
๋ฌธ์ œ์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด 80MB๋ผ๋Š” ์‹์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ณดํ†ต ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” C++์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

(C++ ๊ธฐ์ค€)  int = 4 byte, char = 1 byte, double = 8 byte

 

๋”ฐ๋ผ์„œ, int๋กœ 2์ฒœ๋งŒ์ด ๋Œ€๋žต 80MB๋ผ๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‰ฝ๊ฒŒ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

int a[2์ฒœ๋งŒ] : 80MB
int a[2๋ฐฑ๋งŒ] : 80 / 10 = 8MB
char a[2์ฒœ๋งŒ] : 80 / 4 = 20MB
double a[2์ฒœ๋งŒ] : 80 * 2 = 160MB
 

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


๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ์ถ”๋ก ํ•  ๋•Œ ์ค‘์š”ํ•œ ์ ์€, ๊ฐ’์ด ์ƒ์„ฑ๋˜์—ˆ๋‹ค๊ฐ€ ์—†์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ์— ํ•จ์ˆ˜ ๋‚ด ์ •์˜ํ•œ ๊ฐ’์ด ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜์–ด ์‚ฌ๋ผ์กŒ๋‹ค๋ฉด

๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’๋“ค์ด ๊ฐ€์žฅ ๋งŽ์„ ๋•Œ, ๊ทธ ์‹œ์ ์—์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ณ„์‚ฐ์— ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

function solution(n)
  set list = [n][n]       // (1)
  for i = 0 ... i < n
    list[i][i] = n
  for i = 0 ... i < n
    set tmp = [n-1][n-1]     // (2)
    for j = 0 ... j < n-1
      tmp[j][0] = list[0][j]

 

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋ฐฐ์—ด ์„ ์–ธ์„ ํ•˜๋Š” ๋‘ ๋ถ€๋ถ„๋งŒ ๊ณต๊ฐ„๋ณต์žก๋„์— ์˜ํ–ฅ์„ ๋ฏธ์นฉ๋‹ˆ๋‹ค.

(1)๊ณผ (2)์—์„œ ์„ ์–ธ๋˜๋Š” ๋ฐฐ์—ด์ด ๋™์‹œ์— ์กด์žฌํ•  ๋•Œ, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๋ฉฐ ๊ทธ ์ตœ๋Œ“๊ฐ’์ด ์ด ์ฝ”๋“œ์˜ ๊ณต๊ฐ„๋ณต์žก๋„์ด๋ฏ€๋กœ,

(N^2) + (N^2) = 2(N^2)  =>  O(N^2)

 

์‹œ๊ฐ„๋ณต์žก๋„์™€ ๋™์ผํ•˜๊ฒŒ ์ƒ์ˆ˜๊ฐ’ ๋ฐ ๋‚ฎ์€ ์ฐจ์ˆ˜๋“ค์€ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

 

 

 

์ถœ์ฒ˜ ์ฝ”๋“œํŠธ๋ฆฌ
728x90