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
46
47
48
49
#include <string>
#include <vector>
#include <map>
#include <queue>
#define psi pair<string,int>
#define INF 987654321
using namespace std;
 
vector<int> solution(vector<string> gems) {
    vector<int> answer;
    queue< psi > q;
    int len=INF;
    map<string,int> rec;
    for(int i=0;i<gems.size();i++){
        rec.insert({gems[i],1});
    }
    int n=rec.size();
    rec.clear();
    for(int i=0;i<gems.size();i++){
        //이미 있는 경우
        if (rec.find(gems[i]) != rec.end()){
            rec[gems[i]]++;
        }else{
            rec.insert({gems[i],1});
        }
        q.push({gems[i],i+1});   
        if(rec.size()==n){
            string cstr;
            int cpos;
            while(rec.size()==n){
                cstr=q.front().first;
                cpos=q.front().second;
                q.pop();
                rec[cstr]--;
                if(rec[cstr]==0){
                    rec.erase(cstr);
                }
            }
            if(len>((i+1)-cpos)){
                len=(i+1)-cpos;
                while(!answer.empty())
                    answer.pop_back();
                answer.push_back(cpos);
                answer.push_back(i+1);
            }
        } 
    }
    return answer;
}
cs

출처: https://school.programmers.co.kr/learn/courses/30/lessons/67258 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

출처: https://hwan-shell.tistory.com/263

위의 사진처럼 문제를 풀어서 이미지를 가져왔습니다. 
알고리즘은 간단한 투포인터 문제였지만, 구현 난이도가 좀 있는 문제였습니다. 

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[c++] 양궁대회 (DFS,완전탐색 택1)  (0) 2022.09.21
[c++] 셔틀버스  (1) 2022.09.21
[c++] 숫자게임  (1) 2022.09.20
[c++] 등굣길  (0) 2022.09.19
[c++] 이중우선순위큐  (1) 2022.09.19

+ Recent posts