๐Ÿ“ Coding Test Study

[์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋Œ€๋น„] ์ฝ”ํ…Œ ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ

ibelieveinme 2021. 9. 21. 16:09
728x90

ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ ์ „์— ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋ด์•ผํ•œ๋‹ค.

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ์‹œ๊ฐ„/๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์„ ๋ฒ—์–ด๋‚˜๋Š” ์ง€๋ฅผ ํ™•์ธํ•ด๋ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

* ํƒ์ƒ‰ ํšŸ์ˆ˜๋‹น ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„

1์–ต (100,000,000 = 10^8 ) → 1์ดˆ

10์–ต = 1,000,000,000 = 10^9 → 10์ดˆ

 

์˜ˆ๋ฅผ ๋“ค์–ด int๋Š” 4byte๋กœ -2,147,483,648~ 2,147,483,647 ๋ฒ”์œ„์ด๊ธฐ ๋•Œ๋ฌธ์—

ํฌ๊ธฐ๋กœ ๋ณด๋ฉด "21์–ต"์ด๋ฉฐ for๋ฌธ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด 21์ดˆ๊ฐ€ ์†Œ์š”๋œ๋‹ค.

 

 

* ์‹œ๊ฐ„ ๋ณต์žก๋„ Big-O(๋น…์˜ค) ํฌ๊ธฐ ๋น„๊ต

O(1) < O(log n) < O(n) < O(n log n) < O(N^2) < O(2^n) < O(n!) < O(n^n)

 

 

* N=1์–ต ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋น…์˜คํ‘œ๊ธฐ๋ฒ•์— ๋”ฐ๋ฅธ ์‹คํ–‰์‹œ๊ฐ„

์ฆ‰, ์‹คํ–‰์‹œ๊ฐ„์ด 1์ดˆ๋กœ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค๋ฉด O(N)์˜ ํƒ์ƒ‰๋ฌธ์œผ๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ด๋ด์•ผ ํ•œ๋‹ค.

 

 

* ์‹คํ–‰์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

1) time() ํ•จ์ˆ˜

#include <iostream>
#include <time.h>

int main() {
    int sum = 0;
    time_t start = time(NULL);
    
    for (int i = 0; i < 10000; i++)
        for (int j = 0; j < 10000; j++)
            sum += i * j;    
    
    time_t end = time(NULL);
    cout << "์†Œ์š”์‹œ๊ฐ„: ", << (double)(end - start));
}

 

2) clock() ํ•จ์ˆ˜

#include <iostream>
#include <time.h>

int main() {
    clock_t start = clock();
    
    int sum = 0;
    for (int i = 0; i < 10000; ++i)
        for(int j = 0; j < 10000; ++j)
            sum += 1;
    clock_t end = clock();
    cout << "์†Œ์š” ์‹œ๊ฐ„: " << (double)(end - start) / CLOCKS_PER_SEC);
}

=> ์‹œ์ž‘๋ถ€๋ถ„๊ณผ ๋๋ถ€๋ถ„์˜ ์‹œ๊ฐ„์„ ์ €์žฅํ•ด์„œ ๋บŒ์œผ๋กœ์จ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

* ๊ตฌํ˜„๋ฒ•์— ๋”ฐ๋ฅธ ๋น…์˜ค ๊ณ„์‚ฐ

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์—์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋Š”๊ฑธ๊นŒ? (๋น…-์˜ค)

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ฑ๋Šฅ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ๊ฒŒ ์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„์„ ํ†ตํ•ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‘ ๊ธฐ์ค€์€ ์„œ๋กœ ์ƒ์ถฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋ฌผ๋ก  ๋” ๋น ๋ฅด๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ๋„ ๋” ์ ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ

zoosso.tistory.com

 

728x90