CodeMentor.
Ask AIProblemsLearnC++TCSCore CSHRProgress
0Lv 1
ProblemsArrays

Best Time to Buy and Sell Stock

Easy
LeetCode
3/14
EasyArraysLeetCode #121

Buy Low, Sell High (one transaction)

Track the minimum price so far. At each day, profit = price - minSoFar. Keep max profit.

One pass. No nested loops.

Examples

Example 1

Input

6
7 1 5 3 6 4

Output

5
Example 2

Input

5
7 6 4 3 1

Output

0

Stuck? Take a nudge

C++17Autosaves