Didn't quite understand why this code will overflow?


  • 0
    B

    Thank you~

    class Solution {
    public:
        int rob(vector<int>& nums) {
            if(nums.size() <= 0){
                return 0;
            }
            int prev = nums[0];
            int cur = nums[1];
            for(int i = 2; i < nums.size(); i ++){
                int temp = cur;
                cur = max(prev + nums[i], cur);
                prev = temp;
            }
            return cur;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.