-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path프로그래머스_보석쇼핑.cpp
37 lines (35 loc) · 1.09 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
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <algorithm>
#include <set>
using namespace std;
bool all_have(set<string>& gems_valid,map<string,int>& gems_count){ return gems_valid.size()==gems_count.size();}
vector<int> solution(vector<string> gems) {
vector<int> answer;
vector<tuple<int,int,int>> a;
set<string> gems_valid;
map<string,int> gems_count;
int start=0,end=0;
for(int i=0;i<gems.size();i++) gems_valid.insert(gems[i]);
gems_count[gems[start]]=1;
while(true){
if(!all_have(gems_valid,gems_count)){
end++;
if(end==gems.size()) break;
if(gems_count.find(gems[end])==gems_count.end()) gems_count[gems[end]]=0;
gems_count[gems[end]]++;
}
else{
a.push_back({end-start,start,end});
gems_count[gems[start]]--;
if(gems_count[gems[start]]<=0) gems_count.erase(gems[start]);
start++;
}
}
sort(a.begin(),a.end());
answer.push_back(get<1>(a[0])+1);
answer.push_back(get<2>(a[0])+1);
return answer;
}