본문 바로가기
코딩테스트/Codility

[C++ 풀이] Codility - Lessons 10 (Prime and composite numbers), Flags

by Hwan2 2020. 3. 26.
반응형

 

 

 

 

 

 

 

 

// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    vector<int> v;
    int count = 1, cmp = 0;
    
    for(int i = 1; i < A.size() - 1; i++){
        if(A[i - 1] < A[i] && A[i] > A[i + 1])
            v.emplace_back(i);
    }
        
    if(v.size() < 2) return v.size();
    
    if(A[A.size() - 1] > A[A.size() - 2]){
        v.pop_back();
        v.emplace_back(A.size() - 1);
    }
    
    int flag = 2;
    
    while(true){
        int num = v[0];
        for(int i = 1; i < v.size(); i++){
            if(v[i] - num > flag){
                count++;
                num = v[i];
                if(flag == count){
                    cmp = count;
                    count = 1;
                    break;
                }
            }
        }
        if(flag > cmp)
            return cmp;
        
        flag++;
    }
}​

 

https://app.codility.com/demo/results/trainingWAZP37-GEU/

 

 

한틱 차이로 53점..... 지금은 해결방법이 생각 안나니 나중에 다시 풀어보자...!!

반응형

댓글


스킨편집 -> html 편집에서