121. Best Time to Buy and Sell Stock
来源:互联网 发布:古代西亚北非文明知乎 编辑:程序博客网 时间:2024/06/08 13:11
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 1:
Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.
class Solution {public: int maxProfit(vector<int>& prices) { int min_Now = INT_MAX, max_Now = 0; for(int i = 0; i != prices.size(); ++i){ min_Now = min(min_Now, prices[i]); max_Now = max(max_Now, prices[i] - min_Now); } return max_Now; }};
阅读全文
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- 用Navicat for Mysql导入.sql文件
- Unity游戏开发——UnityUGUI打包图集
- java后台实现发送邮件的功能
- 解码为yuv
- HDU 4734 F(x) 数位DP
- 121. Best Time to Buy and Sell Stock
- 【自定义View】1.View的滑动
- LeetCode 135 : Candy
- CentOS 6.5 下 CDH 5.2.1 集群安装
- 关于使用file上传图片获取图片绝对路径
- HTML初学习
- H5~居中、盒模型、float(浮动)、position(定位)等问题
- BZOJ 4241 历史研究
- iOS 宏定义~RGB(颜色)