算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
来源:互联网 发布:一般淘宝哪些假货多 编辑:程序博客网 时间:2024/05/17 22:31
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
题目求的是最大利润,只需要遍历一遍,将budget与profit分别对应记下便好。
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0; int profit = 0; int budget = prices[0]; for(int i = 0;i < prices.size();++i) { if(budget > prices[i]) { budget = prices[i]; } else { if(profit < prices[i] - budget) profit = prices[i] - budget; } } return profit; }};
阅读全文
0 0
- 算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
- 【Leetcode-Easy-121】Best Time to Buy and Sell Stock
- LeetCode算法问题10 —— Best Time to Buy and Sell Stock with Transaction Fee
- Leetcode —— Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock
- 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
- 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
- 算法分析与设计week03--122.Best Time to Buy and Sell Stock II
- Leetcode学习(21)—— Best Time to Buy and Sell Stock
- (算法分析Week8)Best Time to Buy and Sell Stock[Easy]
- 算法题——Best Time to Buy and Sell Stock II(JAVA)Greedy
- Best Time to Buy and Sell Stock(算法分析week9)
- web笔记(10/13)-----课程内容
- Mybatis介绍
- AJAX 实现省市县地名三级联动
- <错排>codevs 1697 ⑨要写信
- 用户信息的添加与删除
- 算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
- ArcSDE与SQL sevrver的连接、ArcSDE导入数据、ArcGIS Server发布地图服务
- sudo chown -R $(whoami) /usr/local/lib/pkgconfig
- JavaScript定时函数
- BZOJ[3378][Usaco2004 Open]MooFest 狂欢节 树状数组
- HDU1520-Anniversary party(树形dp)
- 学习插入排序
- 体验店员工绩效考核表
- 线性表的插入