algorithm - Maximum Profit given n apples to sell -


Looking at an array of N and sell the value of n integer, such value [i] represents the value But I can be selling 1 apple. Combine maximum revenue which we can generate in any combination by selling N apple which we can choose.

Example: The given 8 apples and value arrays are as follows: -

  {1,5,8,9,10,17,17,20} meaning 1 Apple = $ 1 = $ 4 Total sales price of $ 5, etc. The maximum revenue will be generated by selling 2 + 6 apples for a maximum of 5 + 17 = $ 22.  

I have a simple recursive solution in my mind, but I wonder what is the complexity of the best time that we can achieve? Is there an opportunity to implement dynamic programming or memoison?

Thank you!

definitions

let dp [k] optimum

By the form, DP [0] = 0

DP [K] = Max {DP [I] + Benefits [K -] >

Then we can define, the recurrence relationship is as follows:}, where 0 & lt; = I & lt; k

You can DP [n] . Interested in computing.

Conclusion

You can apply it easily Down-up approach or memoanization and both methods are very fast.

The complexity of the time of this solution is O (n ^ 2) , because calculating an entry in the dp array O ( N) time.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -