Algorithm/C++ - 프로그래머스

프로그래머스 Level2 - 최솟값 만들기 C++

ㅇㅇ잉 2021. 4. 14. 21:28

작은수 * 큰수를 하게끔 A는 오름차순, B는 내림차순으로 정렬하여 각각 답을 구해주면 되는 문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int solution(vector<int> A, vector<int> B)
{
    int answer = 0;
    
    sort(A.begin(), A.end());
    sort(B.rbegin(), B.rend());
    
    for(int i=0;i<A.size();i++){
        answer+=(A[i]*B[i]);
    }
 
    return answer;
}
cs