[Leetcode]Best Time to Buy and Sell Stock
来源:互联网 发布:灵云手写输入软件 编辑:程序博客网 时间:2024/05/16 14:17
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: /*algorithm DP for A[1..i],the biggest value is max(A[1...i]) - min(A[1..i-1)) time O(n) space O(1) */ int maxProfit(vector<int> &prices) { int maxProfit = 0,minPrice = INT_MAX; for(int i = 1;i < prices.size();i++){ minPrice = min(minPrice,prices[i-1]); maxProfit = max(maxProfit,prices[i] - minPrice); } return maxProfit; }};
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
- DSYM 文件定位crash
- IIS 7.5 .Net 4.5 WCF 404.3错误
- Unity游戏编程定制编辑器(一)
- 关于iOS中几种延时方式
- 5 redraw
- [Leetcode]Best Time to Buy and Sell Stock
- bug日记
- 牛X素材推荐之BannerTime
- codeforces 321# E. Kefa and Watch (线段树+字符串hash)
- 内部网关协议RIP
- Linux C pipe函数 man 翻译
- Android 图片二次采样、质量压缩
- node.js 基于事件驱动的回调
- 为什么Nginx的性能要比Apache高很多?