231102 스티커 모으기(2)

Jongleee·2023년 11월 3일
0

TIL

목록 보기
406/576
public int solution(int[] sticker) {
	int len = sticker.length;

	if (len == 1)
		return sticker[0];

	int[] withFirst = new int[len];
	int[] withoutFirst = new int[len];

	withFirst[0] = sticker[0];
	withFirst[1] = sticker[0];
	withoutFirst[0] = 0;
	withoutFirst[1] = sticker[1];

	for (int i = 2; i < len; i++) {
		withFirst[i] = Math.max(withFirst[i - 1], withFirst[i - 2] + sticker[i]);
		withoutFirst[i] = Math.max(withoutFirst[i - 1], withoutFirst[i - 2] + sticker[i]);
	}

	return Math.max(withFirst[len - 2], withoutFirst[len - 1]);
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/12971

0개의 댓글