动态规划之-用局部最优和全局最优实现时间优化
来源:互联网 发布:淘宝如何提高售后评分 编辑:程序博客网 时间:2024/06/07 23:06
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; }}
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[n][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[n][k+1]; int[][] local = new int[n][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]; }}
- 动态规划之-用局部最优和全局最优实现时间优化
- matlab局部最优和全局最优算法
- 动态规划之 最优二叉搜索数 递归实现
- 【动态规划】最优时间表
- 动态规划-最优解
- 动态规划--最优编辑
- 动态规划-最优编辑
- 算法之动态规划最优二叉树
- 动态规划之最优矩阵连乘
- 动态规划之-最优二叉查找树
- 动态规划之最优二叉搜索树
- 动态规划之最优二叉搜索树
- 动态规划之最优二叉搜索树
- 动态规划之最优配对问题
- 动态规划之最优二叉搜索树
- 动态规划之最优二叉搜索树
- 动态规划之最优二叉树
- 动态规划之 最优流水调度问题
- 输入一个按照升序排列的数组和一个数字,在数组中查找两个数,使得他们的和正好是输入的那个数字,要求时间复杂度是O(n),如果有多对数字的和等于输入的数字,输入任意一对即可。
- RxAndroid响应式开发
- spring配置hibernate事务aop不生效的问题
- AtCoder Regular Contest 069 F
- android资源之res/raw和assets的异同
- 动态规划之-用局部最优和全局最优实现时间优化
- com.android.internal.R不存在
- PyTorch: Data Loading and Processing Tutorial 数据加载和处理教程
- Jhipster禁用noliquibase
- Java之基本数据类型(8种还是9种)-yellowcong
- 2.一元多项式相乘
- wget命令
- 格式化磁盘分区详解
- RHCS