본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 마지막 두 원소

by 현장 2025. 2. 8.

-Code

import java.util.Arrays;

class Solution {
    public int[] solution(int[] num_list) {
        int[] answer = Arrays.copyOf(num_list, num_list.length + 1);
        int n1 = num_list[num_list.length - 1];
        int n2 = num_list[num_list.length - 2];
        answer[answer.length - 1] = n1 > n2 ? n1 - n2 : n1 * 2;  

        return answer;
    }
}