removed rod-cutting.cpp
parent
92c144a0e6
commit
bbcabe742f
|
@ -1,108 +0,0 @@
|
|||
/*
|
||||
Problem Statement: Rod Cutting Problem
|
||||
|
||||
Q. We are given a rod of size ‘N’. It can be cut into pieces. Each length of a piece has a particular price given by the price array. Our task is to find the maximum revenue that can be generated by selling the rod after cutting( if required) into pieces.
|
||||
|
||||
Space Optimization
|
||||
|
||||
If we closely look the relation,
|
||||
|
||||
dp[ind][length] = max(dp[ind-1][length] ,dp[ind][length-(ind+1)])
|
||||
|
||||
We see that to calculate a value of a cell of the dp array, we need only the previous row values (say prev). So, we don’t need to store an entire array. Hence we can space optimize it.
|
||||
|
||||
We will be space optimizing this solution using only one row.
|
||||
|
||||
|
||||
Intuition:
|
||||
If we clearly see the values required: dp[ind-1][cap] and dp[ind-1][cap – wt[ind]], we can say that if we are at a column cap, we will only require the values shown in the blue box(of the same column) from the previous row and other values will be from the cur row itself. So why do we need to store an entire array for it?
|
||||
|
||||
If we need only one value from the prev row, there is no need to store an entire row. We can work a bit smarter.
|
||||
|
||||
We can use the cur row itself to store the required value in the following way:
|
||||
|
||||
1.We somehow make sure that the previous value( say preValue) is available to us in some manner ( we will discuss later how we got the value).
|
||||
|
||||
2.Now, let us say that we want to find the value of cell cur[3], by going through the relation we find that we need a preValue and one value from the cur row.
|
||||
|
||||
3.We see that to calculate the cur[3] element, we need only a single variable (preValue). The catch is that we can initially place this preValue at the position cur[3] (before finding its updated value) and later while calculating for the current row’s cell cur[3], the value present there automatically serves as the preValue and we can use it to find the required cur[3] value. ( If there is any confusion please see the code).
|
||||
|
||||
4.After calculating the cur[3] value we store it at the cur[3] position so this cur[3] will automatically serve as preValue for the next row. In this way we space optimize the tabulation approach by just using one row.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int cutRodUtil(vector<int>& price, int ind, int N, vector<vector<int>>& dp){
|
||||
|
||||
if(ind == 0){
|
||||
return N*price[0];
|
||||
}
|
||||
|
||||
if(dp[ind][N]!=-1)
|
||||
return dp[ind][N];
|
||||
|
||||
int notTaken = 0 + cutRodUtil(price,ind-1,N,dp);
|
||||
|
||||
int taken = INT_MIN;
|
||||
int rodLength = ind+1;
|
||||
if(rodLength <= N)
|
||||
taken = price[ind] + cutRodUtil(price,ind,N-rodLength,dp);
|
||||
|
||||
return dp[ind][N] = max(notTaken,taken);
|
||||
}
|
||||
|
||||
|
||||
int cutRod(vector<int>& price,int N) {
|
||||
|
||||
vector<int> cur (N+1,0);
|
||||
|
||||
for(int i=0; i<=N; i++){
|
||||
cur[i] = i*price[0];
|
||||
}
|
||||
|
||||
for(int ind=1; ind<N; ind++){
|
||||
for(int length =0; length<=N; length++){
|
||||
|
||||
int notTaken = 0 + cur[length];
|
||||
|
||||
int taken = INT_MIN;
|
||||
int rodLength = ind+1;
|
||||
if(rodLength <= length)
|
||||
taken = price[ind] + cur[length-rodLength];
|
||||
|
||||
cur[length] = max(notTaken,taken);
|
||||
}
|
||||
}
|
||||
|
||||
return cur[N];
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
vector<int> price = {2,5,7,8,10};
|
||||
|
||||
int n = price.size();
|
||||
|
||||
cout<<"The Maximum price generated is "<<cutRod(price,n);
|
||||
}
|
||||
|
||||
/*
|
||||
Output: The Maximum price generated is 12
|
||||
|
||||
Time Complexity: O(N*N)
|
||||
|
||||
Reason: There are two nested loops.
|
||||
|
||||
Space Complexity: O(N)
|
||||
|
||||
Reason: We are using an external array of size ‘N+1’ to store only one row.
|
||||
|
||||
*/
|
Loading…
Reference in New Issue