Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- redux-toolkit
- sw expert academy
- Algorithm
- useDispatch
- react
- SW
- react-router
- programmers
- JavaScript
- react-redux
- 항해99
- axios
- maeil-mail
- 항해플러스
- createSlice
- 테코테코
- redux
- 코딩테스트합격자되기
- 자바
- C++
- redux-saga
- 이코테
- 리액트
- Get
- java
- 알고리즘
- json-server
- Python
- 프로그래머스
- 매일메일
Archives
- Today
- Total
Binary Journey
[프로그래머스] 최솟값 만들기 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
** Javascript
function solution(A, B){
const a = A.sort((x, y) => x - y);
const b = B.sort((x, y) => y - x);
return a.reduce((acc, curr, index) => acc += curr * b[index], 0);
}
다른 풀이들을 보니까 모두 같은 생각이었나 보다. 아래는 가장 많은 추천을 받은 풀이다.
function solution(A,B){
A.sort((a, b) => a - b)
B.sort((a, b) => b - a)
return A.reduce((total, val, idx) => total + val * B[idx], 0)
}
보니까 sort 의 경우 인스턴스에 리턴값을 받을 필요가 없는 것 같다.
** Python (2021-11-29)
def solution(A,B):
a = sorted(A)
b = sorted(B, key = lambda x : x * -1)
answer = 0
for i in range(len(a)):
answer += a[i] * b[i]
return answer
** Java (2021-11-29)
import java.util.*;
class Solution
{
public int solution(int []A, int []B)
{
Arrays.sort(A);
Arrays.sort(B);
int answer = 0;
for (int i = 0; i < B.length; i++) answer += A[i] * B[B.length - i - 1];
return answer;
}
}
반응형
'프로그래머스 > level 2' 카테고리의 다른 글
[프로그래머스] H-Index (0) | 2021.08.11 |
---|---|
[프로그래머스] 최댓값과 최솟값 (0) | 2021.08.10 |
[프로그래머스] N개의 최소공배수 (0) | 2021.08.10 |
[프로그래머스] 오픈채팅방 (0) | 2021.08.09 |
[프로그래머스] 프로그래머스 124 나라의 숫자 (0) | 2021.04.22 |