How does python judge the best time to buy and sell stocks
This article mainly explains "python how to judge the best time to buy and sell stocks", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python how to judge the best time to buy and sell stocks" bar!
Topic: the best time to buy and sell stocks
Given an array, its first element is the price of a given stock on day I.
Design an algorithm to calculate the maximum profit you can make. You can complete as many trades as you can (buy and sell a stock multiple times).
Note: you cannot participate in multiple trades at the same time (you must sell the previous stock before buying again).
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 7
Explanation: if you buy on day 2 (stock price = 1) and sell on day 3 (stock price = 5), the exchange will earn a profit of 5-1 = 4.
Then, buy on day 4 (stock price = 3) and sell on day 5 (stock price = 6). The exchange earns a profit of 6-3 = 3.
Example 2:
Input: [1, 2, 3, 4, 5]
Output: 4
Tip:
one