Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*javascript max profit buy sell k times*/
- function buySell(arr, k){
- let prof = []
- console.log(arr)
- for(let i =0; i < arr.length; i++){
- prof.push(0)
- }
- console.log("-------")
- console.log(prof)
- for(let j = 0; j < k; j++){
- let min = arr[0]
- let max = 0
- for(let i = 1; i < arr.length; i++){
- min = Math.min(min, arr[i] - prof[i]) //-2
- max = Math.max(max, arr[i]-min) //3, 5-(-2)
- prof[i] = max
- }
- console.log(prof)
- }
- return prof.pop()
- }
- // [0,3,3, 5]
- //[0, 3,3, 7]
- console.log(buySell([3, 3, 5, 0, 0, 3, 1, 4], 2))
- //6
Advertisement
Add Comment
Please, Sign In to add comment