
题目
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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
1 |
Input: [7,1,5,3,6,4] |
Example 2:
1 |
Input: [7,6,4,3,1] |
解决方案
1 |
class : |
算法思想
只能做一次交易,只需要找到全局的最小和全局的最大,那么就能找到答案
复杂度分析
时间复杂度O(n) 一重循环




近期评论