假设你有一个数组prices,长度为n,其中prices[i]是股票在第i天的价格,请根据这个价格数组,返回买卖股票能获得的最大收益
1.你可以买入一次股票和卖出一次股票,并非每天都可以买入或卖出一次,总共只能买入和卖出一次,且买入必须在卖出的前面的某一天
2.如果不能获取到任何利润,请返回0
3.假设买入卖出均无手续费
数据范围: 
要求:空间复杂度
,时间复杂度 )
[8,9,2,5,4,7,1]
5
在第3天(股票价格 = 2)的时候买入,在第6天(股票价格 = 7)的时候卖出,最大利润 = 7-2 = 5 ,不能选择在第2天买入,第3天卖出,这样就亏损7了;同时,你也不能在买入前卖出股票。
[2,4,1]
2
[3,2,1]
0
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], - prices[i]); dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]); } return dp[dp.length - 1][1]; } }
public int maxProfit (int[] prices) { // write code here int min = Integer.MIN_VALUE; for (int i = 0; i < prices.length; i++) { for (int j = i + 1; j < prices.length; j++) { min = Math.max(min, prices[j] - prices[i]); } } if (min < 0) { min = 0; } return min; }
public int maxProfit (int[] prices) { int min = 10000;//记录当前轮之前遇到过的最小值 int max = 0;//记录差值的最大值 for(int i =0;i<prices.length;i++){ max = Math.max(max,prices[i]-min);//更新差值 min = Math.min(min,prices[i]);//更新最小值 } return max; }
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]; //0 买入 1卖出 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],-prices[i]); dp[i][1]=Math.max(dp[i-1][1],dp[i-1][0]+prices[i]); } return dp[prices.length-1][1]; } }
public int maxProfit (int[] prices) { int res = 0; //卖出的那天收益最大,逆向思考需要卖出前某天买入时是最小的,如此一来就能保证在卖出的那天其收益最大,是非负数 //minBuy用于记录卖出前的最小买入值 int minBuy = prices[0]; for (int i = 1; i < prices.length; i++) { res = Math.max(prices[i] - minBuy,res); minBuy=Math.min(prices[i],minBuy); } return res; }- 时间复杂度O(n),空间复杂度O(1)
import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // 创建一个数组,并计算每一天卖出时能得的最大利润,存入数组 // 因为 今天的最大利润 = 今天的价格 - 以前的最低价格 // 且 昨天的最大利润 = 昨天天的价格 - 以前的最低价格 // 所以今天的最大利润 = 今天的价格 - 昨天的价格 + 昨天的最大利润 int[] dp = new int[prices.length]; int res = 0; dp[0] = 0; for (int i = 1; i < prices.length; i++) { dp[i] = dp[i - 1] + prices[i] - prices[i - 1] > 0 ? dp[i - 1] + prices[i] - prices[i - 1] : 0; res = Math.max(res, dp[i]); } return res; } }
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]; int res = 0; dp[0] = 0; for(int i = 1; i < prices.length; i++){ dp[i] = dp[i - 1] + prices[i] - prices[i - 1] > 0 ? dp[i - 1] + prices[i] - prices[i - 1] : 0; res = Math.max(res, dp[i]); } return res; } }
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][prices.length]; for(int i=0;i<=prices.length-1;i++){ Arrays.fill(dp[i],0); } for(int i=0;i<=prices.length-2;i++){ for(int j=i+1;j<=prices.length-1;j++){ if(prices[j]-prices[i]>0){ dp[i][j]=prices[j]-prices[i]; } } } return max(dp); } public int max(int[][] arr){ int max=arr[0][0]; for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[0].length;j++){ max=arr[i][j]>max?arr[i][j]:max; } } return max; } }
import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { int len = prices.length; if(len < 2) { return 0; } int[][] maxProfit = new int[len][2]; // 初始化 maxProfit[0][0] = 0; maxProfit[0][1] = -prices[0]; for (int i = 1; i < len; i++) { maxProfit[i][0] = Math.max(maxProfit[i-1][0],maxProfit[i-1][1] + prices[i]); maxProfit[i][1] = Math.max(maxProfit[i-1][1],-prices[i]); } return maxProfit[len-1][0]; } }
import java.util.*; public class Solution { public int maxProfit (int[] prices) { int n = prices.length; if(n==0 || n==1) return 0; int[] dp = new int[n]; int res = 0, min = Integer.MAX_VALUE; for(int i=0; i<n; i++){ min = Math.min(min, prices[i]); dp[i] = prices[i] - min; res = Math.max(res, dp[i]); } return res; } }
public static int maxProfit(int[] prices){ int n = prices.length; if (n <= 1){ return 0; } int[] dp = new int[n]; dp[0] = 0; int curr = dp[0]; for (int i = 1; i < n; i++) { dp[i] = dp[i-1]; curr = dp[i]; for (int j = 0; j < i; j++) { if (prices[i] > prices[j]){ dp[i] = Math.max(curr,prices[i]-prices[j]); curr = dp[i]; } } } return dp[n-1]; }
if(prices.length==1) return 0; //先求出来每一天之前的股票的最低价格 int[] lowestPrice=new int[prices.length]; lowestPrice[0]=-1; lowestPrice[1]= prices[0]; for (int i=2;i< lowestPrice.length;i++){ lowestPrice[i]=Math.min(lowestPrice[i-1],prices[i-1]); } int res=0; //遍历计算每天和该天之前股票最低价格的差,得到结果 for (int i=1;i< prices.length;i++) res=Math.max(prices[i]-lowestPrice[i],res); return res;