【LeetCode】122. Best Time to Buy and Sell Stock II 解题记录
问题描述
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
测试样例
1 | Input: prices = [7,1,5,3,6,4] |
1 | Input: prices = [1,2,3,4,5] |
1 | Input: prices = [7,6,4,3,1] |
说明
1 | 1 <= prices.length <= 3 * 10^4 |
解题
思路
若当天股票价格比前一天高,则在前一天买入,当天卖出即可。
补充:
- 贪心算法
- 时间复杂度
O(n)
代码
1 | class Solution { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论