Best Time to Sell and Buy Stock

这道题想了很多,但是想多了。这个题思路很简单,如果当前值大于最小值,就计算差,和最大利润值比较。

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         if(prices.size()==0)
 5             return 0;
 6         int length = prices.size();
 7         int minVal = prices[0];
 8         int maxP = 0;
 9
10         for(int i=1;i<length;i++)
11            {
12                if(prices[i]<minVal)
13                  minVal = prices[i];
14                if(prices[i]-minVal>maxP)
15                  maxP = prices[i]-minVal;
16            }
17         return maxP;
18     }
19 };
时间: 2024-12-19 20:05:03

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

[Leetcode] Best Time to Buy Stock

考虑,股票的价格是不断变化的,卖出只能在买入后进行.而且只能买入卖出一次. 开始的时候想扫一遍,求最大最小,但是肯定不会这么简单.你484傻 因为只能卖出一次,若i天卖出,可能的盈利值是, prices[i] - min,就是今天的价格. 再将这个值与之前可能的最大值比较,可以得到 第i天可能的最大值. class Solution { public: int maxProfit(vector<int>& prices) { if (prices.size() <= 1) ret

Best Time to Buy and Sell Stock with Cooldown

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

309. Best Time to Buy and Sell Stock with Cooldown

/* * 309. Best Time to Buy and Sell Stock with Cooldown * 2016-7-4 by Mingyang * http://buttercola.blogspot.com/2016/01/leetcode-best-time-to-buy-and-sell.html * *1. Define States * *To represent the decision at index i: *buy[i]: Max profit till inde

[经典] Best Time to Buy and Sell Stock

这一系列求最优值的问题变种挺多 1. 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 m

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

LeetCode Best Time to Buy and Sell Stock with Cooldown

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: 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

188. Best Time to Buy and Sell Stock IV (Array; DP)

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

[LeetCode] 22. Best Time to Buy and Sell Stock II Java

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