[LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
时间 to and 最佳 Time 股票 lintcode Best
2023-09-11 14:21:37 时间
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.
Example
Given array [3,2,3,1,2]
, return 1
.
LeetCode上的原题,请参见我之前的解法Best Time to Buy and Sell Stock。
class Solution { public: /** * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { int res = 0, mn = INT_MAX; for (int i = 0; i < prices.size(); ++i) { mn = min(mn, prices[i]); res = max(res, prices[i] - mn); } return res; } };
相关文章
- obj-c编程10:Foundation库中类的使用(5)[时间对象]
- MongoDB设置字段过期时间TTL(Time To Live )索引以及修改过期时间
- ORACLE 常用函数——日期/时间函数
- python 中hive 取日期时间的方法
- Flink(22):Flink之Window案例一(基于时间的滚动和滑动窗口)
- LeetCode-380. O(1) 时间插入、删除和获取随机元素【哈希表,动态数组,随机数】
- Centos 6.7 配置时间同步解决时间自动变化的问题
- association in CDS view is converted to LEFT OUTER MANY TO ONE JOIN in the runtime
- 内容分发网络 CDN 是如何提高网页加载时间的?
- BigData:数据中台相关术语概念简介—数据域/业务过程/业务域/指标字典/指标类型/原子指标/派生指标/度量/维度/维度属性/时间周期/修饰词/修饰类型等之详细攻略
- Keras之MLPR:利用MLPR算法(3to1【窗口法】+【Input(3)→(12+8)(relu)→O(mse)】)实现根据历史航空旅客数量数据集(时间序列数据)预测下月乘客数量问题
- 100天精通Python(数据分析篇)——第61天:Pandas.to_datetime函数基础+代码实战(处理时间)
- PAT 1033. To Fill or Not to Fill (贪婪)
- 时间戳 时区 java mysql
- linux 修改文件时间
- 时间序列数据库选型——本质是列存储,B-tree索引,抑或是搜索引擎中的倒排索引
- Java 日期时间