【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()==) return ;
int min_price = prices[], max_profit = ;
for ( size_t i = ; 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
================================================
第二次过这道题,犹豫了一下才想起来思路。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int globalProfit = ;
if ( prices.empty() ) return globalProfit;
int minPrice = prices[];
for ( int i=; i<prices.size(); ++i )
{
minPrice = min(minPrice, prices[i]);
globalProfit = max(globalProfit, prices[i]-minPrice);
}
return globalProfit;
}
};
【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 w ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- 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 a ...
- 【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 a ...
- 【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 a ...
- 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 a ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 【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 ...
随机推荐
- webstorm下载激活汉化
下载 官方下载地址:https://www.jetbrains.com/webstorm/ 激活 参考http://blog.csdn.net/it_talk/article/details/5244 ...
- linux 命令——2 cd (转)
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. ...
- SQL Server Profiler查询跟踪的简单使用
1.打开SQL Server Management Studio,选择工具->SQL Server Profiler,或者直接从路径:开始/程序/Microsoft SQL Server 200 ...
- appuim操作webview控件
1.操作webview控件,在uiautomator中如下图,能定位的只有最外层的内容.就一个webview控件,查找不到里面内容 1.使用driver.getContext(),获取是否是webvi ...
- void (*signal(int sig, void (*func) (int))) (int)理解
http://blog.csdn.net/sever2012/article/details/8281271 1.signal( int sig, void (*func)(int))signal是一 ...
- 2018.5.28 Oracle数据库补充
select * from (select rownum rn,e2.* from (select e1.* from emp e1)e2 where rownum<=10)e3 where e ...
- 启动tomcat的Cannot find ./catalina.sh 的问题
从终端进入tomcat的bin目录,然后执行startup.sh Cannot find bin/catalina.sh The file is absent or does not have exe ...
- React后台管理系统-商品管理列表组件
1.商品列表页面结构 <div id="page-wrapper"> <PageTitle title="商品列表" ...
- React后台管理系统-table-list组件
table-list组件可用于商品列表,用户列表页面 需要传入一个tableHeads集合和tablebody import React from 'react'; // 通用的列表 class ...
- SummerVocation_Leaning--java动态绑定(多态)
概念: 动态绑定:在执行期间(非编译期间)判断所引用的对象的实际类型,根据实际类型调用其相应的方法.如下例程序中,根据person对象的成员变量pet所引用的不同的实际类型调用相应的方法. 具体实现好 ...