Algorithm 15

[프로그래머스] 가장 가까운 같은 글자

문제설명 알고리즘 1. 알파벳과 그 인덱스를 map 에 저장한다 2. map에서 해당 key를 조회하고 있으면 그 value와 문자열을 순회하는 index값을 빼어 결과값에 저장 3. key가 없는 경우 결과값에 -1 저장, 그리고 map에 key값과 현재 index저장 코드 #include #include #include using namespace std; vector solution(string s) { vector answer; map m; for(int i=0; i< s.size(); i++){ char temp = s[i]; if(m.count(temp)){//해당 알파벳이 있으면 answer.push_back(i-m[temp]); //제일 가까운 위치 구해서 거리 저장 m[temp] = i;..

Algorithm 2023.02.24

[프로그래머스] 귤 고르기

문제설명 알고리즘 1. 귤의 크기 별 몇 개 존재하는 지 카운트 -> (key, value) 형태인 map이 가장 적절 2. MAP에 넣은 뒤, 귤의 갯수 기준으로 내림차순 정렬 3. 벡터로 변환하여 벡터 순회하여 k 개에서 최대 귤 수 부터 k 값 감소 시키기 & 결과 값 증가 코드 #include #include #include #include #include using namespace std; bool cmp (const pair &a, const pair &b){ if(a.second == b.second) return a.first > b.first; return a.second > b.second; } int solution(int k, vector tangerine) { int answer..

Algorithm 2023.02.22

[프로그래머스] 명예의 전당(1)

문제설명 알고리즘 1. 점수 값을 조회하면서 다른 벡터에 값을 넣는다 2. 내림차순으로 정렬한다 3. K번째가 되기전까지는 가장 큰 인덱스 값이 최하 값이고, K번째까지 벡터에 들어왔을 경우 K-1번째가 최하값이므로 저장한다 코드 #include #include #include #include using namespace std; vector solution(int k, vector score) { vector answer; vector temp; for(int i=0; i< score.size(); i++){ int current = score[i]; temp.push_back(current); sort(temp.rbegin(), temp.rend()); // 내림차순 정렬 if(i

Algorithm 2023.02.22