[Leetcode] Best Time to Buy and Sell Stock

Problem

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.

Analysis

  • 遍历数组,记录当前最小值,记录当前最大profit
    • 当前profit = 当前值 – 当前最小值
  • 时间复杂度: O(n)
  • 空间复杂度: O(1)

Code

Reference
[1] https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Leave a Reply

Your email address will not be published. Required fields are marked *

*