https://school.programmers.co.kr/learn/courses/30/lessons/12941
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
ํ๋ฆฐ ์ฝ๋
import java.util.*;
import java.util.stream.*;
class Solution
{
public int solution(int []A, int []B)
{
List<Integer> listA = Arrays.stream(A)
.boxed()
.collect(Collectors.toList());
List<Integer> listB = Arrays.stream(B)
.boxed()
.collect(Collectors.toList());
Collections.sort(listA);
Collections.sort(listB, Comparator.reverseOrder());
int answer = 0;
for (int i=0; i<listA.size(); i++) {
answer += listA.get(i) * listB.get(i);
}
return answer;
}
}
๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ์ํด int[] -> List ๋ณํ
์๋ฐ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฐ๋ค๊ณ ํจ.
์ ๋ต ์ฝ๋
import java.util.*;
class Solution
{
public int solution(int []A, int []B)
{
Arrays.sort(A);
Arrays.sort(B);
int size = A.length;
int answer = 0;
for (int i=0; i<size; i++) {
answer += A[i] * B[size-i-1];
}
return answer;
}
}
๋๋ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ => List ๋ฐ stream ๋ถํ์
'๐ค > ์๊ณ ๋ฆฌ์ฆ ์ฌํ์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/Java] ์บ์ (1) | 2025.03.21 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/Java] ์์ (1) | 2025.03.17 |
[ํ๋ก๊ทธ๋๋จธ์ค/Java] ๊ทค ๊ณ ๋ฅด๊ธฐ (1) | 2025.03.12 |