leetcode — 121 买卖股票最佳时机

题目

在这里插入图片描述

题解

代码

	public static int maxProfit2(int[] prices) {
    
    
		int n = prices.length;
		if(prices == null || n == 0)
			return 0;
		
		int[] dp = new int[n];
		dp[0] = 0;
		for(int i=1;i<n;i++) {
    
    
			dp[i] = Math.max(dp[i-1], prices[i] - min(prices, i));
		}
		
		return dp[n-1];
    }
	
	//prices中0-end的最小值
	public static int min(int[] prices, int end) {
    
    
		int min = prices[0];
		for(int i=0;i<end;i++) {
    
    
			if(prices[i] < min) {
    
    
				min = prices[i];
			}
		}
		return min;
	}

猜你喜欢

转载自blog.csdn.net/qq_36281031/article/details/108352442