MaxVal = max(maxVal, prices + dp)įAQs Q.1: What is the time complexity per state transition of the Dynamic Programming approach?Īns: The time complexity is O(n) per transition. By generating all possible configurations of different pieces and finding the maximum among them, we can get our optimal solution.Ĭode / Implementation: C++ Code int maximumProfit(vector & prices, int n) Python Code def maximumProfit(prices, n): We can solve this problem naively with a recursive backtracking-based solution. It can be seen that performing no cuts and taking the entire rod as a whole can lead to the optimal answer of 6. In 4 simple steps you can find your personalised career roadmap in Software development for FREEĬutting the rod into 2 rods of length 2 and 6 gives us a cost of 3 + 9 = 12, which is optimal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |