您的位置:首页 > 其它

动态规划之-用局部最优和全局最优实现时间优化

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.
/*
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];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态规划