int main(){ int Rlen = 4; MaxSub = new int[Rlen]; for(int i = 0; i <= Rlen; i++) MaxSub[i] = Price[i]; printf("%d\n",MaxProfit(Rlen)); }
void TestMaxProfit() { int array[5] = { 1, 2, 1, 3, 3 }; int result = MaxProfit(array, 5); printf("found unique number: %d!\n", result); }
//O(n^2) int MaxProfit(int Rlen){ if(Rlen <= 1) return MaxSub[Rlen]; for(int i = 1;i <= Rlen; i++){ //if the leftmost cut is length i, the new maxsub is the the bigger of those two int tmp = MaxProfit(Rlen - i); //MaxSub[i] uses the value previously calculated if(MaxSub[i] + tmp > MaxSub[Rlen]) MaxSub[Rlen] = MaxSub[i] + tmp; } return MaxSub[Rlen]; }
int main() { int a[] = {5, 10, 3, 6, 4, 15, 1, 2, 9}; std::vector<int> input(a, a + sizeof(a) / sizeof(int)); assert(MaxProfit(input) == 27); return 0; }