코딩테스트/C++
[ 백준 BOJ ] 30804번 - 과일 탕후루 (C++)
최-코드
2025. 3. 4. 16:17
https://www.acmicpc.net/problem/30804
생각 흐름
- 앞, 뒤로 빼는 점에서 투 포인터가 떠올랐다.
#include <iostream>
#include <memory.h>
#include <numeric>
#define endl '\n'
using namespace std;
int N;
int S[200000];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N;
for(int i = 0; i<N; i++){
cin>>S[i];
}
int maxNum=0;
int countKind=0;
int checked[10];
memset(checked, 0, sizeof(checked));
int i = 0, j = 1;
if(S[i]==S[j]){
countKind = 1;
checked[S[i]]+=2;
}else{
countKind = 2;
checked[S[i]]++;
checked[S[j]]++;
}
if(N==1){
cout<<1<<endl;
return 0;
}
while(true){
if(countKind<=2){
if(j==N-1){
break;
}else{
j++;
if(++checked[S[j]]==1){
countKind++;
}
}
}else{
if(--checked[S[i]]==0){
countKind--;
}
i++;
}
if(countKind<=2){
maxNum = max(maxNum, accumulate(checked, checked+10, 0));
}
}
cout<<maxNum<<endl;
return 0;
}