[LeetCode] 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.

这道题相当简单,感觉达不到Medium的难度,只需要遍历一次数组,用一个变量记录遍历过数中的最小值,然后每次计算当前值和这个最小值之间的差值最为利润,然后每次选较大的利润来更新。当遍历完成后当前利润即为所求,代码如下:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if (prices.empty()) return 0;
        int res = 0;
        int minp = prices[0];
        for (int i = 0; i < prices.size(); ++i) {
            minp = min(minp, prices[i]);
            res = max(res, prices[i] - minp);
        }
        return res;
    }
};
时间: 2024-10-01 06:06:06

[LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间的相关文章

[LintCode] 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. Have

[LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

[LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

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 two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不能冲突的,比如说在明天买入,今天卖出.因此,对于今天的价格,应该要找到今天之前的该股的最低价,买入,今天卖出. 其实就是要为序列中的一个元素A[k],找到另一个元素A[e],位置满足e<k,结果使得A[k]-A[e]最大. 用动态规划解决,从左扫起,遇到一个元素就更新当前最小值,再用当前元素去减这个

LeetCode | 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Python】

LeetCode 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机[Easy][Python][贪心] Problem LeetCode 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 (i.e., b

[LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

121. Best Time to Buy and Sell Stock买卖股票12

[抄题]: 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. Input: [7, 1, 5, 3, 6, 4] Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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 two transactions. Note:You may not engage in multiple transactions at the same time (ie,

[LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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, yo