[LeetCode]Best Time to Buy and Sell Stock
来源:互联网 发布:php实现导出excel 编辑:程序博客网 时间:2024/05/17 08:59
解题思路:
记录两个状态
1,当前最大收益maxprofit = max(maxprofit, prices[i] - minCost);
2,以及当前最小花费 minCost = min ( minCost , prices[i] )
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.size() == 0) return 0; int maxProfit = 0; int minCost = prices[0]; for(int i = 1; i < prices.size(); ++i){ maxProfit = max(prices[i] - minCost, maxProfit); minCost = min(minCost, prices[i]); } return maxProfit; }};
0 0
- [LeetCode]Best Time to Buy and Sell Stock
- 【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]Best Time to Buy and Sell Stock II
- 【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
- nyoj236心急的C小加
- eTerms3无法启动
- 玩转Android---UI篇---CheckBox(多选按钮)
- FPGA 里边所说的I/O BANK是个什么概念
- Android Studio系列之Gradle基础
- [LeetCode]Best Time to Buy and Sell Stock
- MyBatis学习总结(一)——MyBatis快速入门
- 命令设计模式
- Nginx、LVS及HAProxy负载均衡软件的优缺点详解
- 从设置字体来看:JAVA面向对象的特点
- 学习网站
- Android控件开发之GridView
- http header 详解
- 玩转Android---UI篇---ListView之SampleAdapter(列表)---1