[LeetCode 题解]:Best Time to Buy and Sell Stock
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
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 maximum profit.
2. 题意
给定一个整数数组Array用以表示某股票在每一天的价格,即该数组中第i个元素表示第i天的价格。
假如用户在给定的天数内只能完成一次交易(即在某一天购买该股票,然后在另一天将其抛售)。请设计一个算法,使得用户在此交易过程中获得最大利润。
3. 思路
规则:一只股票必须先买入,然后再售出。
采用动态规划的思路:
使用两个变量记录当前最小值minPrice,以及当前最大值maxPrice。
Profit的递归过程,可以按照如下状态变换方程表示:

4: 解法
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()<=1) return 0;
int min=prices.front(); // 当前最小值
int max=prices.front(); //当前最大值
int maxP=0; //最大profit
for(int i=1;i<prices.size();i++){
if(prices[i]>max){ //当前值大于max
maxP=prices[i]-min;
max=prices[i];
}else if(prices[i]<min){ // 当前值比min小,那么更新min
min=prices[i];
}else{
if(prices[i]-min>maxP){ // 当前值介于min~max之间
maxP=prices[i]-min;
max=prices[i];
}
}
}
return maxP;
}
};
[LeetCode 题解]:Best Time to Buy and Sell Stock的更多相关文章
- 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 al ...
- [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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 309. 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 al ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- 利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程
1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器 ...
- C# StopWatch的BUG????
//BUG?????? //使用StopWatch测试运行时间 //两段测试A和B //测试结果受测试顺序影响,后测要比先测耗时长了许多 static void TestKeyIntStr() { v ...
- mysql语句与sql语句的基本区别
. MySQL支持enum和set类型,SQL Server不支持: . MySQL不支持nchar.nvarchar.ntext类型: . MySQL数据库的递增语句是AUTO_INCREMENT, ...
- [iOS]隐藏导航栏把右滑退出操作保留
项目因为用到上面导航栏样式多变,就隐藏了导航栏自己用View代替了,但手势却不见了,后来发现问题解决.操作如下: 千万不要取消 Shows Navigation Bar 这个选项否则手势会消失 应该是 ...
- golang之strings
针对golang中的字符串的介绍,提供了一些常用的函数和方法 package main import ( "fmt" "strings" ) func main ...
- ASP.NET Web API(C#)学习01
Web Api 记得去年公司有个分享会分享了这个,最近留意招聘信息的时候,发现有个招聘信息的要求是会用WebApi,然后花了半个小时不到,根据下面这篇文章了解了一下,觉得这个东西也不难啊. 突然发现在 ...
- SQL 组内排序
SELECT t_time, code, name, CL, row_number () OVER (partition BY t_time ORDER BY cl) AS 组内排名1, --T_ti ...
- mysql存储过程(procedure)
#创建带参数的存储过程 delimiter // ),out p int) begin ; end // delimiter call pro_stu_name_pass(@n,@p); select ...
- 【codevs3160】 LCS 【后缀自动机】
题意 给出两个字符串,求它们的最长公共子串. 分析 后缀自动机的基础应用. 比如两个字符串s1和s2,我们把s1建为SAM,然后根据s2跑,找出s2每个前缀的最长公共后缀. 我们可以理解为,当向尾部增 ...
- opencv3.3
--------------------opencv3.2 E:\Opencv\opencv3_2_1\opencv\mybuild\install\includeE:\Opencv\opencv3_ ...