Maximize your profit in the trade market as a developer


Recently, the cryptocurrency market is very interesting. We enjoy making profits from it. But, how can we maximize the profit?

Dynamic Programming

The best way to find the optimal solution is to backtrack the path of getting this solution.

f(n) = g[f(n-1)]


You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k. Find the maximum profit you can achieve. You may complete, at most, k transactions. You must sell the stock before you buy again. (

  • State of buying or selling: when you buy or sell a share, the profit will change
  • Price of day
profit = [{"sell": 0, "buy": -prices[0]} for _ in range(k + 1)]
profit[i]["sell"] = max(
profit[i]["buy"] + prices[day])
profit[i]["buy"] = max(
profit[i - 1]["sell"] - prices[day],)
return max([trans["sell"] for trans in profit])

Another way

Previously, we cache the profits by transactions. We can cache them by days, too.

profit = [[0 for _ in range(days)] for _ in range(3)]


Dynamic Programming can seek the optimal solution. Back to the topic, How can we make more money using it? We need to know the limit of the number of transactions, track the profit when we sell or buy a share, and have the price of the share every day.

Developper in Paris, interested in Big Front-end, Full Stack, Explainable AI. Try to introduce AI into Full Stack workflow