- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of rod lengths called rodLen. We also have another two integers called profit and cost, represents profit per length and cost per cut. We can make gain profit per unit length of a rod but we can only sell rods that are all of the same length. We can also cut a rod into two pieces such that their lengths are integers, but we have to pay cost amount for each cut. We can cut a rod as many times as we want. We have to find the maximum profit that we can make.

So, if the input is like rodLen = [7, 10] profit = 6 cost = 4, then the output will be 82, as we can cut the rod of length 7 into two rods, with length 5 and 2. We can then cut the rod of length 10 into two rods, both with length 5. Then sell all 3 rods of length 5 for a total profit of (5 + 5 + 5) * 6 - (2*4) = 82.

To solve this, we will follow these steps −

- n := size of rodLen
- if n is same as 0, then
- return 0

- l_max := maximum of rodLen
- p_max := 0
- for cuts in range 1 to l_max, do
- p_cut := 0
- for each rod_len in rodLen, do
- if rod_len < cuts, then
- go for next iteration

- c_count := rod_len / cuts
- total_len := c_count * cuts
- if rod_len is same as total_len, then
- c_count := c_count - 1

- curr_profit := total_len * profit - cost * c_count
- if curr_profit < 0, then
- go for next iteration

- p_cut := p_cut + curr_profit

- if rod_len < cuts, then
- p_max := maximum of p_max and p_cut

- return p_max

Let us see the following implementation to get better understanding −

def solve(rodLen, profit, cost): n = len(rodLen) if n == 0: return 0 l_max = max(rodLen) p_max = 0 for cuts in range(1, l_max + 1): p_cut = 0 for rod_len in rodLen: if rod_len < cuts: continue c_count = rod_len // cuts total_len = c_count * cuts if rod_len == total_len: c_count -= 1 curr_profit = total_len * profit - cost * c_count if curr_profit < 0: continue p_cut += curr_profit p_max = max(p_max, p_cut) return p_max rodLen = [7, 10] profit = 6 cost = 4 print(solve(rodLen, profit, cost))

[7, 10], 6, 4

82

- Related Questions & Answers
- Program to find maximum profit after buying and selling stocks at most two times in python
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum profit by cutting the rod of different length in C++
- Maximum profit after buying and selling the stocks in C++
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum length of k ribbons of same length in Python
- Maximum profit by buying and selling a share at most twice
- Find Selling Price from given Profit Percentage and Cost in C++
- Program to find number of items left after selling n items in python
- Program to find maximum element after decreasing and rearranging in Python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
- Program to find maximum binary string after change in python

Advertisements