Dynamic Programming, Easy
Question
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.
Note that you cannot sell a stock before you buy one.
Example 1:
|
|
Example 2:
|
|
Eg:
[7,1,5,3,6,4], difference between adjacent elements is [-6, 5, -2, 3, -2]
The problem is changed to find Largest continuous substring
My Answer
|
|
Running time: $O(n^2)$
With less time:
|
|
Running time: O(n) 4ms