관리 메뉴

fatalite

🏳 겹치는 건 싫어 - 20922번 백준 본문

코딩 인터뷰/C++

🏳 겹치는 건 싫어 - 20922번 백준

fataliteforu 2023. 9. 16. 12:44

문제

문제 난이도 : 실버 1

문제 분류 : 두 포인터

 

문제 풀이 및 코드

복잡...

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_set>
using namespace std;

void Init()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

int main()
{
    //Initialize
    Init();

    //Input
    int n, k = 0;
    cin >> n >> k;
    vector<int> List;
    vector<int> Count(100001,0);
    for (int i = 0; i < n; ++i)
    {
        int Tmp;
        cin >> Tmp;
        List.push_back(Tmp);
    }
    //Main Logic

    int Start = 0;
    int End = 0;
    int Length = 0;
    while (true)
    {
        if (End >= n || Start >= n || Start > End) break;

        if (Count[List[End]] < k)
        {
            Count[List[End]]++;
            End++;
            Length = max(Length, End - Start);
        }
        else if(Count[List[End]] == k)
        {
            Count[List[Start]]--;
            Start++;
        }
    }
    cout << Length;
}

'코딩 인터뷰 > C++' 카테고리의 다른 글

구간 합 구하기 - 2042번 백준  (0) 2023.10.01
섬의 개수 - 4963번 백준  (0) 2023.09.16
점프 점프 - 11060번 백준  (0) 2023.09.14
🏳 쉬운 계단 수 / 10844번 백준  (0) 2023.09.10
연속합 / 1912번 백준  (0) 2023.09.09