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(): 벡터에서 가장 큰 요소를 찾아주는 함수
'Algorithm' 카테고리의 다른 글
[프로그래머스] 코딩테스트 고득점 Kit 힙 C++ (더 맵게, 디스크 컨트롤러, 이중우선순위큐) (1) | 2024.10.26 |
---|---|
[프로그래머스] 코딩테스트 고득점 Kit 정렬 C++ (K번째 수, 가장 큰 수, H-Index) (0) | 2024.10.25 |
[프로그래머스] 코딩테스트 고득점 Kit 해시 C++ (폰켓몬, 완주하지 못한 선수, 전화번호 목록, 의상, 베스트앨범) (3) | 2024.10.24 |
[프로그래머스 level3] 파괴되지 않은 건물 C++ (4) | 2024.10.08 |
[프로그래머스 level3] 이중우선순위큐 C++ (1) | 2024.10.08 |