# The DFS solution of leetcode 123, 188 stock trading problem template

Time：2021-4-29  The common point of these two problems is that there is a limit on the number of transactions, that is to say, there are two states changing: I (the current date is the index of the array) and K.

Here, the state of DFS is found, that is, the parameter of DFS function:
`function dfs (i,k){}`

Actually, it’s here`dfs(i,k)`It is equivalent to using DP array of dynamic programming`dp[i][k]`

every last`dfs(i,k)`There are two internal states, namely, owning stocks and not owning stocks.
Namely`Function DFS (I, K) {return}`

current`dfs(i,k)`The value of shares owned is:`dfs(i-1,k)`(i.e. the day before)Stock ownership status value and`dfs(i-1,k-1)`The status value of not owning stock-`prices[i]`, the best of both.

current`dfs(i,k)`The value of non ownership is:`dfs(i-1,k)`Do not own stock status value and`dfs(i-1,k)`Owning stock status value+`prices[i]`, the best of both.

The final result is:Trading K (or 2) times on the last day and not owning shares

Here is the code for 123. The best time to buy and sell stocks

``````/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
//The solution of the stock problem template based on DFS
//You can use memo to optimize or rewrite directly to DP
function dfs(i,k){
//Critical value
if(i < 0 || k === 0) return [0,Number.MIN_SAFE_INTEGER]
//In the case of current I K,
//1. Non ownership of shares:
//The best of the two: no stock the day before and stock the day before but sell today
//2. Stock ownership:
//There are stocks in the previous day and there are no stocks in the previous day, and they only trade k-1 times (because they will buy when there are no stocks in the previous day)
//The best of both
const status_0 = Math.max(dfs(i-1,k),dfs(i-1,k)+prices[i])
const status_1 = Math.max(dfs(i-1,k),dfs(i-1,k-1)-prices[i])
return [status_0,status_1]
}
//The final result is the situation of trading twice on the last day and not owning stocks
return dfs(prices.length-1,2)

---------------------Dividing line-----------------------

//Next, use the template optimized by DP
const len = prices.length, dp = new Array(len)
for(let i = 0; i<len; i++){
dp[i] = new Array(3)
dp[i] = [0,Number.MIN_SAFE_INTEGER]
}
dp[-1] = new Array(3)
dp[-1].fill([0,Number.MIN_SAFE_INTEGER])
for(let i = 0; i<len; i++){
for(let j = 1; j<=2; j++){
const status_0 = Math.max(dp[i-1][j],dp[i-1][j]+prices[i])
const status_1 = Math.max(dp[i-1][j],dp[i-1][j-1]-prices[i])
dp[i][j] = [status_0,status_1]
}
}
return dp[len-1]
};``````

The best time to buy and sell stocks

This problem is similar to the problem, but the optimization is more difficult than the previous one. It’s easy to time out. DFS is as like as two peas.

``````/**
* @param {number} k
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(k, prices) {
//The solution of the stock problem template based on DFS
//You can use memo to optimize or rewrite directly to DP
function dfs(i,k){
//Critical value
if(i < 0 || k === 0) return [0,Number.MIN_SAFE_INTEGER]
//In the case of current I K,
//1. Non ownership of shares:
//The best of the two: no stock the day before and stock the day before but sell today
//2. Stock ownership:
//There are stocks in the previous day and there are no stocks in the previous day, and they only trade k-1 times (because they will buy when there are no stocks in the previous day)
//The best of both
const status_0 = Math.max(dfs(i-1,k),dfs(i-1,k)+prices[i])
const status_1 = Math.max(dfs(i-1,k),dfs(i-1,k-1)-prices[i])
return [status_0,status_1]
}
//The final result is the last day of trading K times and do not own stocks
return dfs(prices.length-1,k)
};``````

## Dandelion · Jerry technology weekly Vol.21 – technology weekly · React Hooks vs Vue 3 + Composition API

Dandelion · Jerry technology weekly Vol.21 Choose react or Vue, everyone will have their own answer in their heart. There are many reasons to pick the framework of heart water, but when we ask ourselves, can we really evaluate the difference between the two. Perhaps we should return to the original intention and look at […]