본문 바로가기
코딩/cpp

[백준] 30804번

by 적막한숲 2025. 10. 2.

O(n) 정도의 소요시간이 걸린다.

그리고 28byte 정도 변수로 쓰는듯ㅎ다

#include <iostream>
#include <array>

int main() {
    std::array<int, 2> blank = {0};
    std::pair<int, int> currentCount = {0, 0};
    int totalCount = 0;
    int maxCount = 0;

    int n;
    std::cin >> n;
    for (int i = 0; i<n; i++) {
        int _temp;
        std::cin >> _temp;
        if (!blank[0]) {
            blank[0] = _temp;
        } else if (!blank[1] && blank[0] != _temp) {
            blank[1] = _temp;
        } else if (blank[1] != _temp && blank[0] != _temp) {
            if (maxCount < totalCount) {
                maxCount = totalCount;
            }
            blank[0] = currentCount.first;
            blank[1] = _temp;
            totalCount = currentCount.second;
        }
        totalCount++;

        if (currentCount.first != _temp) {
            currentCount.first = _temp;
            currentCount.second = 1;
        } else {
            currentCount.second++;
        }
    }
    if (maxCount < totalCount) {
        maxCount = totalCount;
    }

    std::cout << maxCount << std::endl;
    return 0;
}

'코딩 > cpp' 카테고리의 다른 글

[TIPS] Overloading Function (2)  (0) 2025.10.03
[TIPS] overloading Function (1)  (0) 2025.10.03
[백준] 21736번  (0) 2025.10.02
[백준] 18870번  (0) 2025.10.01
[백준] 18111번  (0) 2025.10.01