
本文共 6093 字,大约阅读时间需要 20 分钟。
���������������������������������
������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������
- ���������������dp[i]���������i������������������������������������
- ������������������������������������i���������������������
- ������i���������������������(dp[i-1])
- ���i������������������������������i-2���������������������nums[i]������dp[i-2] + nums[i]���
- ������dp[i] = max(dp[i-1], dp[i-2] + nums[i])���
���������������
- dp[0] = nums[0]
- dp[1] = max(nums[0], nums[1])
���������������
public int rob(int[] nums) { if (nums.length == 0) { return 0; } int dp_prev_prev = nums[0]; int dp_prev = Math.max(nums[0], nums[1]); int result = dp_prev; for (int i = 2; i < nums.length; i++) { int dp_current = Math.max(dp_prev, dp_prev_prev + nums[i]); dp_prev_prev = dp_prev; dp_prev = dp_current; if (dp_current > result) { result = dp_current; } } return result;}
������������
- ���������������������������������������������������������������������������������������������������������������
������������������������������
���������������������������������������������������������������������������������������������������������������������������
���������������������������
���������������������������
- ���������������n������������������������������������������������������������������������������������������
- ������������������������������������n-1���������������������������n-1������������������
- ���������������fmax(������������������������������������������������������������������������������������������������������������)���
���������������
���������������
public int rob(int[] nums) { if (nums.length == 1) { return nums[0]; } int normal = robLinear(nums); int withoutFirst = robLinear(nums); withoutFirst = robLinear(nums); //������������������������ int withoutLast = robLinear(nums); int result = Math.max(normal, withoutFirst, withoutLast); return result;}
���������������������������������������������������������������������������������������������������������������������
���������������
public int rob(int[] nums) { if (nums.length == 1) { return nums[0]; } int dp1[] = new int[nums.length]; int dp2[] = new int[nums.length]; dp1[0] = nums[0]; dp1[1] = Math.max(nums[0], nums[1]); for (int i = 2; i < nums.length; i++) { dp1[i] = Math.max(dp1[i - 1], dp1[i - 2] + nums[i]); } dp2[nums.length - 1] = nums[nums.length - 1]; dp2[nums.length - 2] = Math.max(nums[nums.length - 1], nums[nums.length - 2]); for (int i = nums.length - 3; i >= 0; i--) { dp2[i] = Math.max(dp2[i + 1], (dp2[i + 2] ?? 0) + nums[i]); } int normal = dp1[nums.length - 1]; int fromLeftToRight = dp1[nums.length - 1]; int fromRightToLeft = dp2[0]; return Math.max(normal, fromLeftToRight, fromRightToLeft);}
������������
- ������������������������������������������������������������������������������������������������
- ���������������������������������������������������
- ������������������O(n)���������������������
������������������������������
������������������������������������������������������������������������������������������
������������������������������������
������������������������������������������������������������������������������������������������������������������������
������������
- ������������������������������������������������������������
- ���������������������������������������������������������������������
���������������������������������������
������������������������������
public int robTree(TreeNode root) { if (root == null) { return 0; } int stealRoot = root.val; int notStealRoot = 0; // Calculate left subtree possibilities int stealLeft = 0, notStealLeft = 0; if (root.left != null) { stealLeft += robTree(root.left); // ������������ // ������������������������������������������������ int stealLeftChildren = 0; if (root.left.left != null) { stealLeftChildren += root.left.left.val; } if (root.left.right != null) { stealLeftChildren += root.left.right.val; } stealLeft += stealLeftChildren; int notStealLeftChildren = 0; if (root.left.left != null) { notStealLeftChildren += robTreeLeft(root.left.left); } if (root.left.right != null) { notStealLeftChildren += robTreeRight(root.left.right); } notStealLeft += notStealLeftChildren; } // Calculate right subtree possibilities int stealRight = 0, notStealRight = 0; if (root.right != null) { stealRight += robTree(root.right); int stealRightChildren = 0; if (root.right.left != null) { stealRightChildren += root.right.left.val; } if (root.right.right != null) { stealRightChildren += root.right.right.val; } stealRight += stealRightChildren; int notStealRightChildren = 0; if (root.right.left != null) { notStealRightChildren += robTreeLeft(root.right.left); } if (root.right.right != null) { notStealRightChildren += robTreeRight(root.right.right); } notStealRight += notStealRightChildren; } stealRoot += notStealLeft + notStealRight; notStealRoot += stealLeft + stealRight; return Math.max(stehlRoot, notStealRoot);}
������������
- ������������������������������������������������������������������
- ���������������������������������������������������������
发表评论
最新留言
关于作者
