leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0213.cpp (560B)


      1 class Solution {
      2   public:
      3     // see 198. House Robber
      4     int rob_single(vector<int> &nums, int start, int end) {
      5         if (end - start <= 0) return 0;
      6         int prev1 = 0, prev2 = 0;
      7         for (int i = start; i < end; i++) {
      8             int tmp = prev1;
      9             prev1 = max(prev2 + nums[i], prev1);
     10             prev2 = tmp;
     11         }
     12         return prev1;
     13     }
     14 
     15     int rob(vector<int> &nums) {
     16         if (nums.size() == 0) return 0;
     17         return max(nums[0] + rob_single(nums, 2, nums.size() - 1), rob_single(nums, 1, nums.size()));
     18     }
     19 };