Skip to content

Instantly share code, notes, and snippets.

@shiponcs
Last active November 3, 2019 16:55
Show Gist options
  • Save shiponcs/9fbd7ce3f58933a865fd377d1c88b385 to your computer and use it in GitHub Desktop.
Save shiponcs/9fbd7ce3f58933a865fd377d1c88b385 to your computer and use it in GitHub Desktop.
DP-rodcutting
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int size;
cin >> size;
int profit[size];
for(int i = 0; i < size; i++) cin >> profit[i];
int dp[size + 1];
memset(dp, 0, sizeof dp);
for(int i = 1; i <= size; i++){
int mxvalue = 0;
for(int j = 1; j <= i; j++){
mxvalue = max(mxvalue, profit[j - 1] + dp[i - j]);
}
dp[i] = mxvalue;
}
cout << dp[size] << '\n';
return 0;
}
#include <iostream>
using namespace std;
int size;
int* pr;
int* sv;
int rodcutting(int n)
{
if(n <= 0) return 0;
int mx = -1;
for(int i = 1; i <= n; i++){
mx = max(mx, pr[i] + rodcutting(n - i));
}
return mx;
}
int main()
{
cin >> size;
pr = new int[size + 1];
for(int i = 1; i <= size; i++) cin >> pr[i];
sv = new int[size + 1];
cout << rodcutting(size) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment