๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

โฌ› [Programmers] [Java] [Level2] ์ตœ์†Ÿ๊ฐ’ ๋งŒ๋“ค๊ธฐ

Dbswnstjd 2024. 1. 3. 16:32

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12941?language=java

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

import java.util.*;

public class ์ตœ์†Ÿ๊ฐ’๋งŒ๋“ค๊ธฐ {
    public static void main(String[] args) {
        int []A = {1,4,2};
        int []B = {5,4,4};

        int answer = 0;

        Arrays.sort(A);
        Arrays.sort(B);
        for(int i=0; i<A.length; i++){
            answer += A[i]*B[A.length - 1 - i];
        }
        System.out.println(answer);
    }
}

์ตœ์†Ÿ๊ฐ’์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ์ž‘์€๊ฐ’ * ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด A๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  B๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ํ›„ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ณฑํ•ด์ฃผ๋ฉด ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.