Skip to content

Commit

Permalink
feat: Add solution for LeetCode problem 213
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteHyun committed Jul 5, 2024
1 parent 781921c commit 358388a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions house-robber-ii/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 213. House Robber II
// https://leetcode.com/problems/house-robber-ii/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/07/06.
//

class Solution {
func rob(_ nums: [Int]) -> Int {
if nums.count < 3 { return nums.max()! }
var current = 0
var previous = 0

// 첫 번째 집을 턴 경우
for element in nums.dropLast() {
(previous, current) = (current, max(element + previous, current))
}

var current2 = 0
var previous2 = 0

// 첫 번째 집을 털지 않은 경우
for element in nums.dropFirst() {
(previous2, current2) = (current2, max(element + previous2, current2))
}

return max(current, previous, current2, previous2)
}
}

0 comments on commit 358388a

Please sign in to comment.