/
스타 수열.cpp
45 lines (40 loc) · 1.08 KB
/
스타 수열.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* problem : https://programmers.co.kr/learn/courses/30/lessons/70130
* time complexity : O(N) // N = |a|
*/
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int solution(vector<int> a) {
int length = a.size();
// numbers[number] = index list
vector<vector<int>> numbers(length);
for(int i=0;i<length;i++){
numbers[a[i]].push_back(i);
}
int answer = 0;
for(int num=0;num<length;num++){
int left = 0;
int cnt = 0;
bool borrow = false;
for(auto index: numbers[num]){
if(borrow){
if(left < index) cnt++; // possible
left = left + 1;
borrow = false;
}
if(left < index) { // use left
cnt++;
left = index + 1;
} else { // borrow
left = index + 1;
borrow = true;
}
}
if(borrow && left < length) cnt++; // possible
answer = max(answer, 2*cnt);
}
return answer;
}