티스토리 뷰
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/72411
problem
인자
- vector<string> orders - 사용자의 개별 매뉴 주문 정보가 담겨있는 배열
- 사이즈는 2이상 20 이하이다.
- 각 원소의 크기는 2이상 10 이하이다.
- vector<int> course - 추가할 코스요리를 구성하는 단품메뉴들의 갯수가 담긴 배열
- 사이즈는 2이상 10 이하이다.
- 각 원소도 2 이상 10 이하이며 오름차순으로 정렬되어 있다.
반환
추가하게 될 코스요리의 메뉴 구성을 문자열 형태로 배열에 담아 리턴한다.
- 배열과 배열에 저장된 문자열은 모두 오름차순으로 정렬
- orders 와 course 는 리턴하는 배열의 크기가 1 이상이 되도록 주어진다
solution
1. 사전순 출력과 같은 값을 구별하기위해 모든 조합을 정렬한다.
2. course 의 사이즈만큼 반복문을 돈다.
2-1. dfs 에 orders 의 값을 하나씩 보내며 모든 조합을 구한다.
2-2. 가장 많이 나온 조합을 찾는다.
2-3. answer 에 push_back 해준다.
3. answer 을 리턴한다.
사용할 라이브러리 & 함수(라이브러리의 함수)
- map - arr['A'] = 1 과 같이 문자와 숫자를 매칭하기 위해 사용
- sort - 정렬을 위해 사용
- vector, string - 기본
사용할 함수 & 변수
- map<string, int> combinationNum - 숫자와 문자 매칭
- dfs - 조합을 구하기 위한 함수
- lev - 현재 dfs 를 실행한 횟수
- s - 시작 지점
- vector<bool> visited - dfs 를 사용하기위한 방문체크 배열
- int targetLev - dfs 를 실행할 횟수(코스요리의 크기)
- int ordersj - 조합을 만들 배열
- vector<string> answer - 리턴할 배열
code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <map> | |
using namespace std; | |
vector<bool> visited(6); | |
string com = ""; | |
void dfs(int s, int lev, int targetLev, string ordersj, map<string, int> &combinationNum) | |
{ | |
if(lev == targetLev) { //탐색 끝 | |
for (int i = 0; i < ordersj.size(); i++) { | |
if (visited[i] == true)com += ordersj[i]; //이번 탐색에서 선택한 문자들 | |
} | |
combinationNum[com]++; //조합이 나온 횟수 | |
com = ""; | |
return; | |
} | |
for (int i = s; i < ordersj.size(); i++) | |
{ | |
if (visited[i] == true) continue; | |
visited[i] = true; | |
dfs(i, lev + 1, targetLev, ordersj, combinationNum); | |
visited[i] = false; | |
} | |
} | |
vector<string> solution(vector<string> orders, vector<int> course) { | |
for(int i = 0 ; i < orders.size(); i++){ | |
sort(orders[i].begin(), orders[i].end()); //정렬 | |
} | |
vector<string> answer; | |
for(int i = 0; i < course.size(); i++){ //각 course 에 따른 값 (최대10) | |
map<string, int> combinationNum; | |
for(int j = 0; j < orders.size(); j++){ //orders 의 문자열마다 조합 (최대1000) | |
dfs(0, 0, course[i], orders[j], combinationNum); | |
} | |
int max = 0; | |
for(auto p : combinationNum){ | |
if (max < p.second) max = p.second; //최대값 | |
} | |
if(max == 1) continue; | |
for(auto p : combinationNum) | |
if(p.second >= max) //최대값보다 큰 모든 값 = 최대값을 가지는 모든값 | |
answer.push_back(p.first); | |
} | |
sort(answer.begin(), answer.end()); | |
return answer; | |
} |
'algorithm > 프로그래머스' 카테고리의 다른 글
[C++] 프로그래머스 lv2-위장 (0) | 2022.06.04 |
---|---|
[C++] 프로그래머스 lv2-전화번호 목록 (0) | 2022.05.21 |
[C++/프로그래머스] 신규 아이디 추천 -2021 KAKAO BLIND RECRUITMENT (0) | 2021.09.20 |
[C++/프로그래머스] 위클리첼린지 4주차 - 직업군 추천하기 (0) | 2021.09.07 |
[python]프로그래머스 위클리 첼린지 1주차 부족한 금액 계산하기 (0) | 2021.09.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- HackCTF
- openCV
- boj
- 나머지
- 쇠막대기
- 카카오 2021 블라인드 테스트
- 직업군 추천
- 4
- web
- 스택
- programmers
- 👼
- 2
- math
- 21758
- 피보나치
- forensic
- c++
- 1
- 7567
- 괄호
- 다익스트라
- 꿀따기
- FIBO
- python
- 프로그레머스
- 백준
- 더하기
- 파이썬
- 넓이
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함