Algorithm
[프로그래머스] 코딩테스트 고득점 Kit 완전탐색 C++ (최소직사각형, 모의고사)
유자바
2024. 11. 19. 19:06
1️⃣ 최소직사각형
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> v1;
vector <int> v2;
int solution(vector<vector<int>> sizes) {
int answer = 0;
for (int i=0;i<sizes.size();i++){
if (sizes[i][0] >= sizes[i][1]){
v1.push_back(sizes[i][0]);
v2.push_back(sizes[i][1]);
} else {
v1.push_back(sizes[i][1]);
v2.push_back(sizes[i][0]);
}
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
answer = v1[v1.size()-1] * v2[v2.size()-1];
return answer;
}
- 생각 흐름
- 가로 세로 비교해서 큰 숫자는 v1, 작은 숫자는 v2 벡터에 저장
- sort하고 제일 뒤에 있는 숫자끼리 곱하기
2️⃣ 모의고사
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> arr{{1,2,3,4,5},{2,1,2,3,2,4,2,5},{3,3,1,1,2,2,4,4,5,5}};
vector<int> cnt {0,0,0};
vector<int> solution(vector<int> answers) {
vector<int> answer;
for (int i=0;i<answers.size();i++){
if (answers[i] == arr[0][i%arr[0].size()]) cnt[0]++;
if (answers[i] == arr[1][i%arr[1].size()]) cnt[1]++;
if (answers[i] == arr[2][i%arr[2].size()]) cnt[2]++;
}
int max = *max_element(cnt.begin(), cnt.end());
for (int i=0;i<3;i++){
if (max == cnt[i]) answer.push_back(i+1);
}
return answer;
}
💡 *max_element(): 벡터에서 가장 큰 요소를 찾아주는 함수