[LeetCode] Best Time to Buy and Sell Stock
来源:互联网 发布:c语言点餐系统 编辑:程序博客网 时间:2024/05/18 22:40
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.
class Solution {public: int maxProfit(vector<int> &prices) { int n = prices.size(); if(n == 0) return 0; int min = prices[0], profit = 0; for(int i = 1; i < n; i++) { profit = prices[i] - min > profit ? prices[i] - min : profit; min = min < prices[i] ? min : prices[i]; } return profit; }};
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
- Android多国语言使用须知
- MyEclipse快捷键(各种版本)
- 黑马程序员_Java基础(4)--多线程
- ContentProviderTest
- 《算法导论》笔记 第21章 总结与思考
- [LeetCode] Best Time to Buy and Sell Stock
- C语言基础—复杂数据类型
- SVN控制参考文章
- LeetCode Unique Paths
- [LeetCode] Best Time to Buy and Sell Stock II
- 免费80端口穿透之nat123端口映射原理及使用方法
- UVA 10453 Make Palindrome(区间简单DP)
- 笔记之Prolog
- MySQL报错处理