|
| 1 | +class Solution |
| 2 | +{ |
| 3 | + public: |
| 4 | + // Recursive function to calculate the maximum number of cuts possible |
| 5 | + int solve(int n, int x, int y, int z){ |
| 6 | + // Base case: If the length is exactly 0, no further cuts are needed |
| 7 | + if(n == 0) return 0; |
| 8 | + // Base case: If the length becomes negative, this is an invalid case |
| 9 | + if(n < 0) return INT_MIN; |
| 10 | + |
| 11 | + // Try cutting a segment of length x and recursively calculate the cuts |
| 12 | + int a = solve(n-x, x, y, z) + 1; |
| 13 | + // Try cutting a segment of length y and recursively calculate the cuts |
| 14 | + int b = solve(n-y, x, y, z) + 1; |
| 15 | + // Try cutting a segment of length z and recursively calculate the cuts |
| 16 | + int c = solve(n-z, x, y, z) + 1; |
| 17 | + |
| 18 | + // Return the maximum of the three possible cuts |
| 19 | + return max(a, max(b, c)); |
| 20 | + } |
| 21 | + |
| 22 | + // Function to find the maximum number of cuts that can be made |
| 23 | + int maximizeTheCuts(int n, int x, int y, int z) |
| 24 | + { |
| 25 | + // Call the recursive function to calculate the maximum cuts |
| 26 | + int maxCuts = solve(n, x, y, z); |
| 27 | + // If the result is negative, it means no valid cuts are possible |
| 28 | + if(maxCuts < 0) return 0; |
| 29 | + |
| 30 | + // Return the maximum number of valid cuts |
| 31 | + return maxCuts; |
| 32 | + } |
| 33 | +}; |
0 commit comments