์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/์ž๋ฐ”

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค / ์ž๋ฐ” / Lv.2] 12941 ์ตœ์†Ÿ๊ฐ’ ๋งŒ๋“ค๊ธฐ

HHRR 2024. 7. 11. 13:47
๋ฌธ์ œ

ํ’€์ด

 

๋‘ ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ์˜ ๊ณฑ์˜ ๋ˆ„์ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

๊ฐ ์›์†Œ์˜ ๊ณฑ์˜ ๋ˆ„์ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋ ค๋ฉด, ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋ฅผ ๊ณฑํ•ด์ค€ ํ›„์— ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋ฉด ์ตœ์†Ÿ๊ฐ’์ด ๋œ๋‹ค.

 

1. ์ž๋ฐ”์—์„œ๋Š” int[] ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ, ์ผ๋‹จ ๊ฐ๊ฐ A, B ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ด์ค€๋‹ค.

2. A๋ฐฐ์—ด์€ ์›์†Œ์˜ ์‹œ์ž‘์ธ i ๋ถ€ํ„ฐ, B ๋ฐฐ์—ด์€ ์›์†Œ์˜ ๋์ธ (len-1)-i ๋ถ€ํ„ฐ ์—ญ์ˆœ์œผ๋กœ ์›์†Œ๋ฅผ ๊ณฑํ•ด์ฃผ๊ณ , answer์— ํ•ฉ์„ ๋ˆ„์ ํ•œ๋‹ค. 

 

class Solution{
        public int solution(int []A, int []B){
            int len = A.length;
            int answer = 0;
            Arrays.sort(A);
            Arrays.sort(B); // int[] ๋ฐฐ์—ด์€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์ด ์•ˆ๋จ
            for(int i=0; i<len; i++){
                answer += A[i] * B[(len-1)-i]; // B๋ฐฐ์—ด์€ ์—ญ์ˆœ์œผ๋กœ ๊ณฑํ•ด์ฃผ๊ธฐ
            }
            return answer;
        }
    }