121. Best Time to Buy and Sell Stock LeetCode
来源:互联网 发布:淘宝充气娃娃实际拍图 编辑:程序博客网 时间:2024/05/17 08:39
题意:给出每天股票的价格,只能进行一次交易,求最大收益。
题解:DP,minprice表示1…到i天价格最低的股票,maxprice表示1…i天最大收益。maxprice = max(maxprice,price[i] - minprice).
class Solution {public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n == 0) return 0; int minprice = prices[0]; int maxx = 0; for(int i = 1; i < n; i++) { minprice = min(minprice,prices[i]); maxx = max(maxx,prices[i] - minprice); } return maxx; }};
0 0
- LeetCode 121. Best Time to Buy and Sell Stock
- [Leetcode] 121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 121. Best Time to Buy and Sell Stock LeetCode
- [LeetCode]121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- LeetCode题解:121. Best Time to Buy and Sell Stock
- LeetCode *** 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- #leetcode#121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- 121.[Leetcode]Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- CMAKE自动构建
- 从graphite中删除字段信息
- Linux deepin 15.1配置java环境
- HDU 1085 Holding Bin-Laden Captive!(母函数)
- UnityNetwork
- 121. Best Time to Buy and Sell Stock LeetCode
- 46. Permutations LeetCode
- USACO Milking Cows 解题日志
- K-近邻分类算法(监督式)
- HDFS详细讲解
- poj 2378 简单树形dp
- 24. Swap Nodes in Pairs LeetCode
- Git教程
- BZOJ 3306|树|树链剖分