LeetCode: Best Time to Buy and Sell Stock
来源:互联网 发布:淘宝怎么更换支付宝 编辑:程序博客网 时间:2024/06/07 23:09
思路:记录到当前位置i为止,最小值的位置 min_index, 则到当前位置最大收益就是max( 已有收益, prices[i] - prices[index])。
code:
class Solution {public: int maxProfit(vector<int> &prices) { int len = prices.size(); if(len == 0)return 0; int min_v = prices[0], min_index = 0; int ret = 0; for(int i = 1;i < len;i++){ if(min_v > prices[i]){ min_v = prices[i]; min_index = i; } ret = max(ret,prices[i] - prices[min_index]); } return ret; }};
0 0
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock
- 新网络时代学习方式的大变革
- opencv在Ubuntu下的编译及错误汇总
- java安全之SSL
- andrid定义Button的样式和选中样式
- selenium处理 <a target=_blank 几种方式整理
- LeetCode: Best Time to Buy and Sell Stock
- 用apktool和dex2jar反编译
- 正则表达式详解
- 9. 多线程 Part 4 生产者及消费者模式 --- 学习笔记
- JAVA的动态代理
- 访谈录它的重要性
- VS2005下编译openssl
- 嵌入式Linux系统实现3G网卡拨号
- android摄像头编程心得