【Best Time to Buy and Sell Stock】cpp

题目:

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.

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
            if (prices.size()==0) return 0;
            int min_price = prices[0], max_profit = 0;
            for ( size_t i = 0; i < prices.size(); ++i )
            {
                min_price = std::min(min_price, prices[i]);
                max_profit = std::max(max_profit, prices[i]-min_price);
            }
            return max_profit;
    }
};

tips:

Greedy

核心在于维护两个变量:

min_price: 算上当前元素的最小值

max_profit:算上当前元素的最大利润

走完所有元素,可以获得max_profit

时间: 2024-08-04 12:42:19

【Best Time to Buy and Sell Stock】cpp的相关文章

leetcode 【 Best Time to Buy and Sell Stock 】python 实现

思路: 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-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-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

leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

题目: 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)

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

题目: 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 (

【Best Time to Buy and Sell Stock II】cpp

题目: 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)

【Best Time to Buy and Sell Stock III 】cpp

题目: 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 (

【LeetCode】Best Time to Buy and Sell Stock II

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 sha

【LeetCode】【Python题解】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,