-
[프로그래머스/탐욕법/Lv.1] 체육복프로그래머스/탐욕법(Greedy) 2020. 6. 25. 21:03
문제 설명
코드
#include <string> #include <vector> #include <algorithm> using namespace std; int solution(int n, vector<int> lost, vector<int> reserve) { int answer = 0; bool check[31]={false,}; answer = n-lost.size(); if(lost.size()==0 || reserve.size()==0) return answer; sort(lost.begin(),lost.end()); sort(reserve.begin(),reserve.end()); for(int i=0;i<lost.size();i++){ if(find(reserve.begin(),reserve.end(),lost[i])!=reserve.end()){ check[lost[i]]=true; answer++; } } for(int i=0;i<lost.size();i++){ if(check[lost[i]]); else if(lost[i]>1 && check[lost[i]-1]==false && find(reserve.begin(),reserve.end(),lost[i]-1)!=reserve.end()){ check[lost[i]-1]=true; answer++; }else if(lost[i]<n && check[lost[i]+1]==false && find(reserve.begin(),reserve.end(),lost[i]+1)!=reserve.end()){ check[lost[i]+1]=true; answer++; } } return answer; }
소감
네이버 블로그를 사용하다가 티스토리로 옮기게 되었다.
검색엔진이 구글인 편이 댓글로 피드백 달아주실 분들도 많아질 것 같기도 하고..
티스토리가 좀 더 깔끔해서 옮겨보려고 한다.
올렸던 게시물들은 심심할 때마다 옮길 예정이다.
이 문제는 레벨 1이라고 우습게 봤다가 의외로 생각할 게 많아서 고생했다.
우선 문제에 없는 조건들을 캐치해 전부 적용해 주어야 한다.
예를 들면 입력 벡터들이 오름차순 정렬이 되어 있지 않는 경우도 있고,
잃어버린 사람과 여벌을 가진 사람이 같은 경우도 있다.