문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/12941
코딩테스트 연습 - 최솟값 만들기
길이가 같은 배열 A, B 두개가 있습니다. 각 배열은 자연수로 이루어져 있습니다. 배열 A, B에서 각각 한 개의 숫자를 뽑아 두 수를 곱합니다. 이러한 과정을 배열의 길이만큼 반복하며, 두 수를 곱
programmers.co.kr
정렬만 하면 쉽게 풀 수 있는 문제였습니다.
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 <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int solution(vector<int> A, vector<int> B) | |
{ | |
int answer = 0; | |
sort(A.begin(), A.end()); | |
sort(B.begin(), B.end()); | |
for (int i = 0; i < A.size(); i++) | |
{ | |
answer += A[i] * B[B.size() - (i + 1)]; | |
} | |
return answer; | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] JadenCase 문자열 만들기 (0) | 2022.03.13 |
---|---|
[Programmers] 행렬의 곱셈 (0) | 2022.03.12 |
[Programmers] 주차 요금 계산 (0) | 2022.03.06 |
[Programmers] 최댓값과 최솟값 (0) | 2022.03.05 |
[Programmers] 숫자의 표현 (0) | 2022.03.05 |