来啊抢劫啊198. House Robber

来源:互联网 发布:淘宝劳保用品 编辑:程序博客网 时间:2024/05/17 06:05

题目如下:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

大概就是说 你抢劫就抢劫吧 没事别连着抢 不然GG 

借鉴了网上大神的思想 用到了动态规划

大概介个样子

      money[0]=0;
      money[1]=0;
      for(int t=0;t<numsSize;t++)
      {
        int old=money[0];
        if(money[0]>money[1])
            money[0]=money[0];
        else money[0]=money[1];      
        money[1]=old+nums[t];
      }   
        
        if(money[0]>money[1])
            max=money[0];
        else max=money[1];

return max;




原创粉丝点击