Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์†Œ์ˆ˜ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์†Œ์ˆ˜ ์ฐพ๊ธฐ

WonderJay 2022. 7. 6. 22:42
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/12921

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ํšจ์œจ์„ฑ์„ ๊น๊นํ•˜๊ฒŒ ๋ณด๊ธฐ ๋•Œ๋ฌธ์— ์—๋ผํ† ์Šค์ฒด๋„ค์Šค์˜ ์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋Œ€๋ถ€๋ถ„ TLE ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

#include <bits/stdc++.h>
#define fastio cin.tie(0)->sync_with_stdio(0)
#define ll long long
using namespace std;


int solution(int n) {
    int answer = 0; 
    vector<int> arr(n+1, 0);
    fastio;

    for (int i = 2; i <= n; i++)
    {
        if (arr[i] != 0) continue;
        for (int j = i + i; j <= n; j += i) arr[j] = 1;
    }
    for (int i = 2; i <= n; i++)
        if (arr[i] == 0) answer++;

    return answer;
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments