
😎풀이
- 한 집만 존재하면 반드시 털어야 함
- 원형이므로 첫 집과 마지막 집을 털 경우 두가지를 생각
- 도둑질은 두 경우의 수를 갖음
3-1. 전 집을 털자
3-2. 현재 집을 털자
- 첫 집을 털은 최댓값과 마지막 집을 털은 최댓값 비교
- 최댓값 출력
function rob(nums: number[]): number {
if(nums.length === 1) return nums[0]
const includeFirst = nums.slice(0, nums.length - 1)
const includeLast = nums.slice(1)
function robbing(houses: number[]) {
let prevMax = 0
let doublePrevMax = 0
for(const house of houses) {
let temp = prevMax
prevMax = Math.max(doublePrevMax + house, prevMax)
doublePrevMax = temp
}
return prevMax
}
return Math.max(robbing(includeFirst), robbing(includeLast))
};