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

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

[PGS] Lv.0 (์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ž…๋ฌธ) 3์ผ์ฐจ ๋ฌธ์ œ

728x90

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

 

๋‚˜๋จธ์ง€ ๊ตฌํ•˜๊ธฐ

int solution(int num1, int num2) {
    int answer = -1;
    answer = num1 % num2;
    return answer;
}

 

์ค‘์•™๊ฐ’ ๊ตฌํ•˜๊ธฐ

int solution(vector<int> array) {
    int answer = 0;
    int len = array.size() + 1;
    sort(array.begin(), array.end());
    answer = array[len / 2 - 1];
    return answer;
}

 

์ตœ๋นˆ๊ฐ’ ๊ตฌํ•˜๊ธฐ

โœ๐Ÿป ๊ฐ’์„ ๊ตฌํ•ด์•ผํ•˜๋Š”๋ฐ ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด์„œ ์‚ฝ์งˆํ•ด์„œ ์ข€ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค,.. ์ผ๋‹จ ๋ฐฐ์—ด๋ฌธ์ œ๋Š” ์ •๋ ฌ๋ถ€ํ„ฐ ํ•˜๊ณ  ์‹œ์ž‘ํ•˜๊ธฐ!!

โœ๐Ÿป ๊ทธ๋ฆฌ๊ณ  for๋ฌธ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ• ๋•Œ๋Š” ํ•ญ์ƒ 0์ธ ์˜ˆ์™ธ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค

int solution(vector<int> array) {
    int answer = 1;
    int cnt[101] = {0,};
    for(int i=0; i<101; ++i) { cnt[i] = 1;}
    int max = 1;
    int j=0;
    
    sort(array.begin(), array.end());
    if(array.size()==1) return array[0];
    for(int i=1; i<array.size(); ++i){
        if(array[i-1] == array[i]) {
            cnt[j]++;
        }
        else {
            j++;
        }
        if(max <= cnt[j]) {
            answer = array[i];
            max = cnt[j];
        }
    }
    sort(cnt, cnt+j+1);
    if(cnt[j-1] == cnt[j]) return -1;
    return answer;
}

 

์ง์ˆ˜๋Š” ์‹ซ์–ด์š”

vector<int> solution(int n) {
    vector<int> answer;
    for(int i=1; i<=n; ++i){
        if(i%2 == 1) answer.push_back(i);
    }
    return answer;
}

.

โœ๐Ÿป #include <algorithm>

๋ฐฐ์—ด ์ •๋ ฌ ⇒ sort( arr, arr + 10 );

๋ฒกํ„ฐ ์ •๋ ฌ ⇒ sort( v.begin(), v.end() );

728x90