动态规划之-用局部最优和全局最优实现时间优化
2017-11-03 14:51
344 查看
198. 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.
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
the contiguous subarray
152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
the contiguous subarray
188. Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
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.
/* state local[i] 打劫前i家房子,包括第i家房子所能获得的最大收益 global[i] 打劫前i家的最大收益 function:local[i] = max(global[i-2],global[i-3])+nums[i-1] intialization: */ class Solution { public int rob(int[] nums) { if (nums == null || nums.length == 0) return 0; if(nums.length == 1) return nums[0]; if(nums.length == 2) return Math.max(nums[0],nums[1]); int[] local = new int[3]; int[] global = new int[3]; local[0] = global[0] = 0; local[1] = global[1] = nums[0]; local[2] = global[2] = Math.max(nums[0],nums[1]); for(int i = 3; i < nums.length+1; i++){ local[i%3] = Math.max(global[(i-2)%3],global[(i-3)%3]) + nums[i-1]; global[i%3] = Math.max(global[(i-1)%3],local[i%3]); } return global[nums.length%3]; } }
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
[-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray
[4,-1,2,1]has the largest sum =
6.
/* local[] /一定取第i各元素的序列的最大和 max //前i个元素的序列的最大和 */ class Solution{ public int maxSubArray(int[] nums){ int n = nums.length; int[] local = new int[2]; local[0] = nums[0]; int max = nums[0]; for(int i = 1; i < n; i++){ local[i%2] = nums[i] + (local[(i-1)%2]>0?local[(i-1)%2]:0); max = Math.max(max,local[i%2]); } return max; } }
152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
[2,3,-2,4],
the contiguous subarray
[2,3]has the largest product =
6.
/* max[] //一定取第i个元素的序列的最大乘积 min[] //一定取第i个元素的序列的最小乘积 global //前i个元素的最大乘积(可取可不取) */ class Solution { public int maxProduct(int[] nums) { int[] max = new int[2]; int[] min = new int[2]; max[0] = min[0] = nums[0]; int global = nums[0]; for(int i = 1; i < nums.length; i++){ max[i%2] = Math.max(Math.max(max[(i-1)%2] * nums[i],min[(i-1)%2] * nums[i]),nums[i]); min[i%2] = Math.min(Math.min(max[(i-1)%2] * nums[i],min[(i-1)%2] * nums[i]),nums[i]); global = Math.max(max[i%2],global); } return global; } }
188. Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
/* state: local[i][j] 表示前i天,至多进行j次交易,第i天必须sell的最大收益 global[i][j]表示前i天,至多进行j此交易,第i天可以不sell的最大收益 function: gain = prices[i]-prices[i-1] local[i][j] = max(global[i-1][j-1]+gain,local[i-1][j]+gain) global[i][j] = max(global[i-1][j],local[i][j]) intialization: global[0][i] = 0 local[0][i] = 0; answer:global [k] */ class Solution { public int maxProfit(int k, int[] prices) { if(prices.length == 0) return 0; int n = prices.length; //分为两种情况,当k>=n/2时,可以进行最大次数的交易。就是随便买,随便卖 if(k >= n/2){ int maxPro = 0; for(int i = 1; i < n; i++) maxPro += (prices[i]>prices[i-1] ?prices[i]-prices[i-1]:0); return maxPro; } //第二种情况 int[][] global = new int [k+1]; int[][] local = new int [k+1]; for(int i = 0;i <= k; i++){ local[0][i] = 0; global[0][i] = 0; } for(int i = 1; i < n; i++){ int gain = prices[i] - prices[i-1]; for(int j = 1; j <= k; j++){ local[i][j] = Math.max(global[i-1][j-1],local[i-1][j])+gain; global[i][j] = Math.max(global[i-1][j],local[i][j]); } } return global[n-1][k]; } }
相关文章推荐
- 流水线调度最优问题(装配线调度问题)动态规划 O(n)时间(线性时间)C++实现
- 流水线调度最优问题(装配线调度问题)动态规划 O(n)时间(线性时间)C++实现
- 如何实现页面时间实时动态更新(局部更新)
- 类斐波那契问题中动态规划的时间复杂度优化
- 【算法竞赛入门经典】集合的动态规划;时间优化 例题9-16 UVa1252
- 动态规划-寻找最优分配时间。(未理解)
- 动态规划之 最优二叉搜索数 递归实现
- struts2 实现全局校验point类,局部校验时间格式
- 动态规划初步-01背包问题&&一维数组(空间复杂度优化)
- 动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
- 动态规划中的并行优化
- 动态规划:java实现最大字段和问题
- asp.net 实现动态显示当前时间(不用javascript不考虑开销)
- Java动态规划 实现最长公共子序列以及最长公共子字符串
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”
- 动态规划之0-1背包问题(C实现)
- 动态规划实践,美团面试题算法实现
- C++实现矩阵链乘法利用动态规划及运行实例结果
- 动态规划----优化编辑器问题
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance