problem
solution
- backtracking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Solution {
public:
int ret;
void dfs(int k, int t, int prod){
if(t==0){
ret = max(ret, prod);
return;
}
if(t<0) return ;
for(int i=1;i<k;++i){
dfs(k,t-i, prod*i);
}
}
int integerBreak(int n) {
ret = 1;
int prod =1;
dfs(n,n,prod);
return ret;
}
};
option 1 - dp
1 | class Solution { |
option 2 - Math
1 | class Solution { |
analysis
- option 1
- time complexity
O(n^2)
- space complexity
O(n)
- time complexity
- option 2
- time complexity
O(n)
- space complexity
O(1)
- time complexity