[프로그래머스] 스티커모으기(2)

2020. 4. 7. 16:28프로그래머스/LEVEL 4

import java.util.*;

class Solution {
    public int solution(int sticker[]) {
        
        if(sticker.length==1){
            return sticker[0];
        }
        int answer = 36;
        int temp1 = 0;
        int temp2 = 0;
        int[] dp = new int[sticker.length];
        
        dp[0] = sticker[0];
        dp[1] = dp[0];
        
        for(int i=2; i<dp.length-1; i++){
            dp[i] = Math.max(dp[i-1],dp[i-2]+sticker[i]);
        }
        temp1 = dp[dp.length-2];
        
        dp[0] = 0;
        dp[1] = sticker[1];
        
        for(int i=2; i<dp.length; i++){
            dp[i] = Math.max(dp[i-1],dp[i-2]+sticker[i]);
        }
        temp2 = dp[dp.length-1];
        
        answer= Math.max(temp1,temp2);
        return answer;
    }
}