ORIGINAL CODING QUESTION
Repeat inventory trade gain
Buy and sell repeatedly but hold at most one unit at a time. No fees or cooldown apply. Return the greatest total gain.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n integer values. Print one integer as described.
Constraints
1 ≤ n ≤ 200; 0 ≤ price ≤ 100.
Example 1
Input
6 7 1 5 3 6 4
Output
7
Trades 1→5 and 3→6 yield 7.
Example 2
Input
4 1 2 3 4
Output
3
The rising run yields 3.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Several small gains can equal one long rising trade.
Hint 2
Falling edges provide no gain.
Hint 3
Sum positive adjacent price changes.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →