-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
34 lines (30 loc) · 762 Bytes
/
main.cpp
File metadata and controls
34 lines (30 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <unordered_map>
// 1 based prices
int rodHelper(int n, std::vector<int> prices, std::unordered_map<int, int> memo) {
if (n == 0) {
return 0;
}
if (memo.count(n) > 0) {
return memo[n];
}
int max = 0;
for (int i = 0; i < n; i++) {
// take full rod from one size, empty rod on another
int temp = prices[n - i] + rodHelper(i, prices, memo);
if (temp > max) {
max = temp;
}
}
memo[n] = max;
return max;
}
int maxRodCut(int n, std::vector<int> prices) {
std::unordered_map<int, int> data;
return rodHelper(n, prices, data);
}
int main() {
std::cout << maxRodCut(3, {0, 1, 4, 5}) << std::endl;
return 0;
}