121 Best time to buy and sell stock i

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) {
            return 0;
        }

        int min = Integer.MAX_VALUE;
        int max = 0;
        for(int i : prices) {
            min = i < min ? i : min;
            max = (i - min) > max ?(i - min) : max;
        }
        return max;
    }
}

参考:http://blog.csdn.net/linhuanmars/article/details/23162793

“这道题求进行一次交易能得到的最大利润。如果用brute force的解法就是对每组交易都看一下利润,取其中最大的,总用有n*(n-1)/2个可能交易,所以复杂度是O(n^2)。
很容易感觉出来这是动态规划的题目,其实跟Maximum Subarray非常类似,用“局部最优和全局最优解法”。思路是维护两个变量,一个是到目前为止最好的交易,另一个是在当前一天卖出的最佳交易(也就是局部最优)。递推式是local[i+1]=max(local[i]+prices[i+1]-price[i],0), global[i+1]=max(local[i+1],global[i])。这样一次扫描就可以得到结果,时间复杂度是O(n)。而空间只需要两个变量,即O(1)。”

public int maxProfit(int[] prices) {
    if(prices==null || prices.length==0)
        return 0;
    int local = 0;
    int global = 0;
    for(int i=0;i<prices.length-1;i++)
    {
        local = Math.max(local+prices[i+1]-prices[i],0);
        global = Math.max(local, global);
    }
    return global;
}
时间: 2024-10-05 18:15:24

121 Best time to buy and sell stock i的相关文章

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

leetCode 121. Best Time to Buy and Sell Stock 数组

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

[email&#160;protected] [121]Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock Total Accepted: 69292 Total Submissions: 206193 Difficulty: Medium Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction

【leetcode】121. Best Time to Buy and Sell Stock

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) /************************ * @description: dynamic programming. * 相邻元素做差,然后转化

121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

LeetCode 121: Best Time to Buy and Sell Stock

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit

Leetcode 121. Best Time to Buy and Sell Stock JAVA语言

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

(Array)121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. publ

LeetCode OJ 121. Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. [思路]

121. Best Time to Buy and Sell Stock (Array)

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 思路:注