【Leetcode】【Medium】Best Time to Buy and Sell Stock
2024-09-02 06:40:15
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) {
int size = prices.size();
int minV = INT_MAX;
int maxP = ; for (int i = ; i < size; ++i) {
minV = min(minV, prices[i]);
maxP = max(maxP, prices[i] - minV);
}
return maxP;
}
};
最新文章
- [转]Patch文件结构详解
- matplotlib 和 pandas 两个包的安装
- 关于JNI程序中引用另外一个lib
- poj 2406Power Strings
- JavaEE开发之记事本完整案例(SpringBoot + iOS端)
- Java入门篇(二)——Java语言基础(下)
- IOS空数据页面,网络加载失败以及重新登陆View的封装(不需要继承)
- .NET应用程序管理服务AMS设计
- 在windows上搭建git server Gitblit
- jemter的使用
- Spark源码剖析 - SparkContext的初始化(八)_初始化管理器BlockManager
- Linux下IP SAN共享存储操作记录
- Vulkan入门流程
- iOS 设置 UIWebView UserAgent
- eclipse添加源码的另外一种方法
- Ex1—vlookup
- 在线升级java的版本
- MyBatis 事务源码分析
- makedown学习笔记(以后可能会用makedown写博客)
- ruby编码说明