Make Unreal REAL.
article thumbnail
Level 2. 최솟값 만들기

 

 

A 배열의 작은 수부터, B 배열의 큰 수부터 차례로 곱해 더하면 된다.

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int solution(vector<int> A, vector<int> B)
{
    sort(A.begin(), A.end());
    sort(B.rbegin(), B.rend());
    
    transform(A.begin(), A.end(), B.begin(), A.begin(), ::multiplies<int>());

    return accumulate(A.begin(), A.end(), 0);
}

 

없을 줄 알았는데 내적을 계산해주는 inner_product() 함수가 STL에 있었다.

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int solution(vector<int> A, vector<int> B)
{
    sort(A.begin(), A.end());
    sort(B.rbegin(), B.rend());
    
    return inner_product(A.begin(), A.end(), B.begin(), 0);
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 2. 스킬트리  (0) 2023.04.06
Level 2. 추억 점수  (0) 2023.04.05
Level 2. 큰 수 만들기  (0) 2023.04.03
Level 2. 다음 큰 숫자  (0) 2023.04.02
Level 2. 숫자의 표현  (0) 2023.04.01
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그