zoukankan      html  css  js  c++  java
  • House Robber

    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.

    一维DP即可。

     1 public class Solution {
     2     public int rob(int[] num) {
     3         if(num == null || num.length == 0) return 0;
     4         int len = num.length;
     5         int[] dp = new int[len + 1];
     6         dp[1] = num[0];
     7         for(int i = 2; i <= len; i ++){
     8             dp[i] = Math.max(dp[i - 1], dp[i - 2] + num[i - 1]);
     9         }
    10         return dp[len];
    11     }
    12 }
  • 相关阅读:
    网络之传输层
    局域网的物理组成
    网络基础
    RAID磁盘阵列
    mount挂载和交换分区swap
    Linux文件系统
    sed命令基础2
    sed命令基础
    LVM基础
    磁盘配额基础
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4395341.html
Copyright © 2011-2022 走看看