Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Camera Zoom
- css framework
- express
- MySQL
- draganddrop
- java
- react
- OverTheWire
- rpg server
- spread 연산자
- Unity Editor
- Digital Ocean
- critical rendering path
- docker
- Camera Movement
- mongoDB
- Google Refund
- unity
- nodejs
- server
- Google Developer API
- Spring Boot
- springboot
- --watch
- linux
- Git
- Packet Network
- Unity IAP
- screencapture
- SDK upgrade
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 3. 보석 쇼핑 본문
https://school.programmers.co.kr/learn/courses/30/lessons/67258
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
- 보석이 총 몇종류인지 구한다. (totals.size())
- 진열대를 돌면서 , counts에 보석개수를 증가한다. counts는 내가 head 부터 i까지 보석 개수를 의미한다.
- i번째를 구매하고, head에서부터 뺄수있는지 검사하여 head를 당길 수 있다면 당긴다. head에 있는 보석이 2개이상 이라면, head번째는 구매하지않고 head+1부터 구매해도 상관없다는 의미이다.
- counts 에 있는 보석의 종류가 totals.size() 와 동일하다면 답을 갱신해준다. 최솟값을 찾는것이므로, 최솟값일때만 답을 갱신해준다.
#include <string>
#include <vector>
#include <map>
#include <set>
#include <climits>
using namespace std;
vector<int> solution(vector<string> gems) {
vector<int> answer(2,INT_MAX);
map<string,int> counts;
set<string> totals;
for(int i= 0; i<gems.size(); i++){
totals.insert(gems[i]);
}
int head = 0;
for(int i=0; i<gems.size(); i++){
counts[gems[i]] += 1;
while(true){
string gem = gems[head];
if(counts[gem] <= 1) break;
counts[gem]--;
head++;
}
if(counts.size() == totals.size()){
if(answer[0] == INT_MAX || i-head < answer[1]-answer[0]) {
answer[0] = head; answer[1] = i;
}
}
}
answer[0] += 1;
answer[1] += 1;
return answer;
}
'Algorithm(c++) > Level 3' 카테고리의 다른 글
[프로그래머스] Level 3. 가장 먼 노드 (0) | 2025.03.05 |
---|---|
[프로그래머스] Level 3. 징검다리 건너기 (0) | 2025.03.03 |
[프로그래머스] Level 3. 불량 사용자 (0) | 2025.03.01 |
[프로그래머스] Level 3. 스티커 모으기 (0) | 2025.03.01 |
[프로그래머스] Level 3. 기지국 설치 (0) | 2025.02.27 |