|
| 1 | +class Solution |
| 2 | +{ |
| 3 | + public: |
| 4 | + // Function to calculate the maximum number of cuts using an iterative approach |
| 5 | + int solve(int n, int x, int y, int z){ |
| 6 | + // Initialize a dp array with size (n+1) and set all values to INT_MIN |
| 7 | + // INT_MIN is used to indicate invalid states where no cuts are possible |
| 8 | + vector<int> dp(n+1, INT_MIN); |
| 9 | + |
| 10 | + // Base case: If the length is 0, no cuts are needed |
| 11 | + dp[0] = 0; |
| 12 | + |
| 13 | + // Iterate through all lengths from 1 to n |
| 14 | + for(int i = 1; i <= n; i++){ |
| 15 | + // If a cut of length x is possible, update dp[i] |
| 16 | + if(i-x >= 0) dp[i] = max(dp[i], dp[i-x] + 1); |
| 17 | + |
| 18 | + // If a cut of length y is possible, update dp[i] |
| 19 | + if(i-y >= 0) dp[i] = max(dp[i], dp[i-y] + 1); |
| 20 | + |
| 21 | + // If a cut of length z is possible, update dp[i] |
| 22 | + if(i-z >= 0) dp[i] = max(dp[i], dp[i-z] + 1); |
| 23 | + } |
| 24 | + |
| 25 | + // Return the maximum number of cuts for length n |
| 26 | + // If no valid cuts are possible, dp[n] will still be INT_MIN |
| 27 | + return dp[n]; |
| 28 | + } |
| 29 | + |
| 30 | + // Function to calculate the maximum number of cuts that can be made |
| 31 | + int maximizeTheCuts(int n, int x, int y, int z) |
| 32 | + { |
| 33 | + // Call the solve function to calculate the maximum cuts |
| 34 | + int maxCuts = solve(n, x, y, z); |
| 35 | + |
| 36 | + // If the result is negative, it means no valid cuts are possible |
| 37 | + if(maxCuts < 0) return 0; |
| 38 | + |
| 39 | + // Return the maximum number of valid cuts |
| 40 | + return maxCuts; |
| 41 | + } |
| 42 | +}; |
0 commit comments