LeetCode 121. Best Time to Buy and Sell Stock
来源:互联网 发布:为什么淘宝网打不开 编辑:程序博客网 时间:2024/05/29 19:14
Best Time to Buy and Sell Stock
题目描述:
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.
题目代码:
class Solution {public: int maxProfit(vector<int>& prices) { int ans = 0; for(int i = 0; i < prices.size(); i++){ for(int j = i+1; j < prices.size(); j++){ if(prices[j] > prices[i]){ ans = max(prices[j]-prices[i],ans); } } } return ans; }};
阅读全文
1 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
- 白盒测试大纲
- 迪丽热巴终于剪对了刘海!这么选刘海你的发型还有的救
- VPS独享主机技术原理
- 手机号归属地查询接口
- 一些常用css属性
- LeetCode 121. Best Time to Buy and Sell Stock
- epoll中accept的使用细节
- LintCode 379:Reorder array to construct the minimum number
- vue 路由之间传值 (编程式导航)
- PostgreSQL数据库从入门到精通
- DB2错误及解决方法
- 获取当前时间赋给data-
- json: java生成json数据示例
- 敏捷测试流程