算法设计Week9 LeetCode Algorithms Problem #121. Best Time to Buy and Sell Stock
来源:互联网 发布:linux 定时唤醒 编辑:程序博客网 时间:2024/05/18 01:12
题目描述:
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) { if(prices.empty()) return 0; int profit = 0; int price_in = prices[0]; for(int i = 1; i < prices.size(); i++){ profit = max(profit, prices[i] - price_in); if(prices[i] < price_in){ price_in = prices[i]; } } return profit; }};
0 0
- 算法设计Week9 LeetCode Algorithms Problem #121. Best Time to Buy and Sell Stock
- 算法设计Week5 LeetCode Algorithms Problem #122 Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock(算法分析week9)
- Algorithms—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. 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
- Extreme (II)
- java中的接口(interface)
- Retrofit 的使用
- CODEVS 1166 矩阵取数游戏
- qscoj 66 ||2017 UESTC Training for Data Structures D(离线+树状数组)
- 算法设计Week9 LeetCode Algorithms Problem #121. Best Time to Buy and Sell Stock
- [leetcode]: 476. Number Complement
- HDOJ 3045 Picnic Cows(斜率优化DP)
- 原生ajax
- Android 画画板
- Maven实战之Web项目部署
- java中的final关键字
- SpringBoot实战 之 异常处理篇
- bzoj1453: [Wc]Dface双面棋盘