题目分析:动态规划就行,下面的链接我觉得解释的很详细,很适合学习
C++:
int rob_optimization(vector<int>& nums) {
int m_pre = 0;
int m_tail = 0;
int temp = 0;
for (auto x : nums){
temp = max(m_tail , m_pre + x);
m_pre = m_tail;
m_tail = temp;
}
return temp;
}
Python:
def rob(self, nums: List[int]) -> int:
m_pre = 0
m_tail = 0
temp = 0
for x in nums:
temp = max(m_tail, m_pre + x)
m_pre = m_tail
m_tail = temp
return temp