House Robber Solutions in Java
Number 198
Difficulty Easy
Acceptance 42.0%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Memory Search/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution1 {// the max profit for robbing nums[i...n)private int[] memo;public int rob(int[] nums) {memo = new int[nums.length];Arrays.fill(memo, -1);return tryRob(nums, 0);}private int tryRob(int[] nums, int index){if(index >= nums.length)return 0;if(memo[index] != -1)return memo[index];return memo[index] =Math.max(tryRob(nums, index + 1),nums[index] + tryRob(nums, index + 2));}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution1()).rob(nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19/// Dynamic Programming/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution2 {public int rob(int[] nums) {int n = nums.length;if(n == 0)return 0;// the max profit for robbing nums[i...n)int[] memo = new int[nums.length];memo[n - 1] = nums[n - 1];for(int i = n - 2 ; i >= 0 ; i --)memo[i] = Math.max(memo[i + 1],nums[i] + (i + 2 < n ? memo[i + 2] : 0));return memo[0];}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution2()).rob(nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19/// Dynamic Programming with O(1) Space/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution3 {public int rob(int[] nums) {int n = nums.length;if(n == 0)return 0;int curMax = 0, preMax = 0;for(int i = n - 1 ; i >= 0 ; i --) {int temp = curMax;curMax = Math.max(curMax, nums[i] + preMax);preMax = temp;}return curMax;}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution3()).rob(nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Memory Search/// Another way to define the states/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution4 {// the max profit for robbing nums[0...i]private int[] memo;public int rob(int[] nums) {memo = new int[nums.length];Arrays.fill(memo, -1);return tryRob(nums, nums.length - 1);}private int tryRob(int[] nums, int index){if(index < 0)return 0;if(memo[index] != -1)return memo[index];return memo[index] =Math.max(tryRob(nums, index - 1),nums[index] + tryRob(nums, index - 2));}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution4()).rob(nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19/// Dynamic Programming/// Another way to define the states/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution5 {public int rob(int[] nums) {int n = nums.length;if(n == 0)return 0;// the max profit for robbing nums[0...i]int[] memo = new int[nums.length];memo[0] = nums[0];for(int i = 1 ; i < n ; i ++)memo[i] = Math.max(memo[i - 1],nums[i] + (i - 2 >= 0 ? memo[i - 2] : 0));return memo[n-1];}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution5()).rob(nums));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/house-robber/description//// Author : liuyubobobo/// Time : 2017-11-19/// Dynamic Programming with O(1) space/// Another way to define the states/// Time Complexity: O(n)/// Space Complexity: O(1)public class Solution6 {public int rob(int[] nums) {int n = nums.length;if(n == 0)return 0;int curMax = 0, preMax = 0;for(int i = 0 ; i < n ; i ++) {int temp = curMax;curMax = Math.max(curMax, nums[i] + preMax);preMax = temp;}return curMax;}public static void main(String[] args) {int nums[] = {2, 1};System.out.println((new Solution6()).rob(nums));}}