[プログラマ][レベル.2]エンコードテスト-最高値の作成(Javascript)
1508 ワード
質問する
ソース:https://programmers.co.kr/learn/courses/30/lessons/12941
説明する function solution(A, B) {
let answer = 0;
let minNum, minIndex, maxNum, maxIndex;
for (let i = A.length; i > 0; i--) {
A.map((num, index) => {
if (index === 0) {
minNum = num;
minIndex = index;
}
if (num < minNum) {
minNum = num;
minIndex = index;
}
});
B.map((num, index) => {
if (index === 0) {
maxNum = num;
maxIndex = index;
}
if (num > maxNum) {
maxNum = num;
maxIndex = index;
}
});
answer += minNum * maxNum;
A.splice(minIndex, 1);
B.splice(maxIndex, 1);
}
return answer;
}
function solution(A, B) {
let answer = 0;
let minNum, minIndex, maxNum, maxIndex;
for (let i = A.length; i > 0; i--) {
A.map((num, index) => {
if (index === 0) {
minNum = num;
minIndex = index;
}
if (num < minNum) {
minNum = num;
minIndex = index;
}
});
B.map((num, index) => {
if (index === 0) {
maxNum = num;
maxIndex = index;
}
if (num > maxNum) {
maxNum = num;
maxIndex = index;
}
});
answer += minNum * maxNum;
A.splice(minIndex, 1);
B.splice(maxIndex, 1);
}
return answer;
}
他人の解答 function solution(A,B){
let answer = 0;
A.sort((a,b)=> a-b);
B.sort((a,b)=> b-a);
for(let i=0; i<A.length; i++){
answer = answer + A[i]*B[i];
}
return answer;
}
function solution(A,B){
let answer = 0;
A.sort((a,b)=> a-b);
B.sort((a,b)=> b-a);
for(let i=0; i<A.length; i++){
answer = answer + A[i]*B[i];
}
return answer;
}
Reference
この問題について([プログラマ][レベル.2]エンコードテスト-最高値の作成(Javascript)), 我々は、より多くの情報をここで見つけました https://velog.io/@lgs2722/프로그래머스-Level.2코딩테스트-최솟값-만들기-Javascriptテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol