121. Best Time to Buy and Sell Stock
来源:互联网 发布:网络安全员考试2017 编辑:程序博客网 时间:2024/06/01 09:45
1、题目描述
输入一个数组,表示股票的价格变动。只能做一次交易,问最大收益。
2、思路
遍历数组,记录最小值,数组每个值都与它之前出现的最小值做减法,求得差值最大的值即为最大收益
3、代码
int maxProfit(vector<int>& prices) { int ans=0; int n = prices.size(); if(n==0) return 0; int m = prices[0]; for(int i=1;i<n;i++){ if(prices[i]<m) m=prices[i]; ans=max(ans,prices[i]-m); } return ans; }
阅读全文
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
- java线程安全总结
- mysql ERROR 1045 (28000): (using password: YES)问题解决
- navigator对象
- 导入项目出现
- jdbc
- 121. Best Time to Buy and Sell Stock
- JDBC
- 手把手教你用Hexo+(Coding/GitHub)搭建个人博客及绑定私有域名
- java IO流学习总结
- Javascript中事件对象event和e
- android 6.0版本适配的几个重要方面
- java实现SP00LING假脱机输入输出技术模拟
- 数据透视表 笔记
- 哈尔滨理工大学第七届程序设计竞赛决赛(网络赛-高年级组)D 数圈圈【DFS||数位DP】