思路(還有臉說思路?)
- First though 太複雜 is graph. Each house have four connections with its neighbors. e.g. House 4 with House 1, 2, 6, 7.
- We can make all houses a big circle while each house have four lines with four other houses.
- Has to begin in the first three houses and end in the last three houses.
- Path weight is the money of houses before this path
- Find a path from the start to the end to maximum path weight
- See solutions. Here is the main thought
- https://discuss.leetcode.com/topic/28369/the-correct-dp-solution
- M(k) = money at the kth house
- P(0) = 0
- P(1) = M(1)
- P(k) = max(P(k−2) + M(k), P(k−1))
- Third one: space O(1)
- Get rid of array but only constant variables to store previous money
- See my post https://discuss.leetcode.com/topic/66402/java-o-n-runtime-and-o-1-space-before-simplified-version-after-simplified-version
Logic Flaws
- Forget the house 1 and 2, if use P(k - 2)
Failed Cases
- []
- [0]
- [1, 1, 1]