假设你有一个数组prices,长度为n,其中prices[i]是某只股票在第i天的价格,请根据这个价格数组,返回买卖股票能获得的最大收益
1. 你可以多次买卖该只股票,但是再次购买前必须卖出之前的股票
2. 如果不能获取收益,请返回0
3. 假设买入卖出均无手续费
数据范围:
,
要求:空间复杂度
,时间复杂度 )
进阶:空间复杂度
,时间复杂度
[8,9,2,5,4,7,1]
7
在第1天(股票价格=8)买入,第2天(股票价格=9)卖出,获利9-8=1 在第3天(股票价格=2)买入,第4天(股票价格=5)卖出,获利5-2=3 在第5天(股票价格=4)买入,第6天(股票价格=7)卖出,获利7-4=3 总获利1+3+3=7,返回7
[5,4,3,2,1]
0
由于每天股票都在跌,因此不进行任何交易最优。最大收益为0。
[1,2,3,4,5]
4
第一天买进,最后一天卖出最优。中间的当天买进当天卖出不影响最终结果。最大收益为4。
总天数不大于200000。保证股票每一天的价格在[1,100]范围内。
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int[][] dp = new int[prices.length][2]; dp[0][0] = -prices[0]; dp[0][1] = 0; for (int i = 1; i < prices.length; i++) { dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]); dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]); } return dp[prices.length - 1][1]; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here if(prices==null||prices.length==1) return 0; int[][] dp=new int[prices.length][2]; dp[0][0]=-prices[0]; dp[0][1]=0; for(int i=1;i<prices.length;i++){ dp[i][0]=Math.max(dp[i-1][0],dp[i-1][1]-prices[i]); dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]+prices[i]); } return dp[prices.length-1][1]; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int[][] dp = new int[prices.length][2]; for (int i = 0; i < prices.length; i++) { if (i == 0) { dp[0][0] = 0; dp[0][1] = -prices[0]; } else { dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); } } return dp[prices.length - 1][0]; } }
动态规划不是使用一个一维数组也可以吗,为啥官方动态规划要个二维数组。 import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int len = prices.length; //第i位置表示前i天收入的最大值 int[] dp = new int[len +1]; dp[0] = 0; dp[1] = 0; for(int i = 2;i<=len;i++){ //第i天收入的最大值要么是前i-1天收入的最大值,要么第i天大涨,比第i-1 天的还要高,所以加上差值(可正可负)。。 dp[i] = Math.max(dp[i-1],dp[i-1] + prices[i-1] - prices[i-2]); } return dp[len]; } }
import java.util.*; public class Solution { public int maxProfit (int[] prices) { if (prices == null || prices.length <= 1) { return 0; } // 收入 int in = 0; for (int i = 0; i < prices.length - 1; i++) { in += Math.max(prices[i + 1] - prices[i], 0); } return in; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int profit=0; //int[] temp=new int[prices.length-1]; for(int i=1;i<=prices.length-1;i++){ int temp=prices[i]-prices[i-1]; if(temp>0){ profit=profit+temp; } } return profit; } }
import java.util.*; public class Solution { public int maxProfit (int[] prices) { // write code here if(prices.length==0||prices.length==1) return 0; int len=prices.length; int[] profits = new int[len]; for(int i=0;i<len-1;i++){ profits[i]=prices[i+1]-prices[i]; } int res=0; for(int i=0;i<len-1;i++){ if(profits[i]>0){ res+=profits[i]; } } return res; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { int maxProfit = 0, len = prices.length; if(len < 1) return 0; for(int i = 1; i < len; i++){ if(prices[i] > prices[i-1]){ maxProfit += (prices[i] - prices[i-1]); } } return maxProfit; } }
import java.util.*; /** 思路就是:从第一个开始找,如果找到下一个的值比该值大,那么就从下一个值再一直向后 遍历,遍历到最峰值,就跳出循环,同时获取利润 */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { // write code here int profit = 0 ; if(prices.length == 1){ return 0; } for(int i = 0 ; i < prices.length ; i ++){ if(i == prices.length - 1){ break; } //如果当前值比后一个值小, if(prices[i] < prices[i + 1] ){ //就从后一个值一直向后遍历,找到最顶峰的值,也就是找到不满足该值小于下一个值了 int j = i + 1; while(j < prices.length - 1 && prices[j] < prices[j + 1]){ j ++ ; //继续 } profit += prices[j] - prices[i]; // 跳出循环后,表示找到峰值,直接求利润 i = j;//让下一次循环从该值的下一个值开始 } } return profit; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算最大收益 * @param prices int整型一维数组 股票每一天的价格 * @return int整型 */ public int maxProfit (int[] prices) { int n=prices.length; if(n==0) return 0; if(n==1) return 0; int host=-prices[0]; int nohost=0; for(int i=1;i<n;i++){ host=Math.max(host,nohost-prices[i]); nohost=Math.max(nohost,host+prices[i]); } return nohost>0?nohost:0; } }
public int maxProfit (int[] prices) { // write code here if(prices==null || prices.length < 2) return 0; int profit = 0; int current = prices[0]; for(int i = 1; i < prices.length; i++){ if(current < prices[i]){ profit += prices[i] - current; } current = prices[i]; } return profit; }
public int maxProfit (int[] prices) { // write code here if (prices == null || prices.length <= 1) return 0; int maxProfit = 0; int minSoFar = prices[0]; for (int i = 1; i <= prices.length - 1; i++) { if (prices[i] > minSoFar) { maxProfit += prices[i] - minSoFar; minSoFar = prices[i]; } else { minSoFar = Math.min(minSoFar, prices[i]); } } return maxProfit; }