class Solution {
public:
int rob(vector<int>& nums) {
vector<int> ret;
int mm = 0;
for (int i = 0; i < nums.size(); i++)
{
int m = max(i>=2?(ret[i-2]+nums[i]):nums[i], i>=1?ret[i-1]:0);
ret.push_back(m);
if (m > mm)
{
mm = m;
}
}
return mm;
}
int max(int a, int b)
{
return a>b?a:b;
}
};
public:
int rob(vector<int>& nums) {
vector<int> ret;
int mm = 0;
for (int i = 0; i < nums.size(); i++)
{
int m = max(i>=2?(ret[i-2]+nums[i]):nums[i], i>=1?ret[i-1]:0);
ret.push_back(m);
if (m > mm)
{
mm = m;
}
}
return mm;
}
int max(int a, int b)
{
return a>b?a:b;
}
};
评论
发表评论