-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicProg1.cpp
42 lines (32 loc) · 1.07 KB
/
dynamicProg1.cpp
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
35
36
37
38
39
40
41
42
// Given 3 numbers {1, 3, 5}, the task is to tell the total number of ways we can form a
// number N using the sum of the given three numbers. (allowing repetitions and
// different arrangements).
// https://www.geeksforgeeks.org/introduction-to-dynamic-programming-data-structures-and-algorithm-tutorials/
#include <iostream>
// recursive, complexity = O(3^n)
int solve(int n) {
if (n < 0)
return 0;
if (n == 0)
return 1;
else
return solve(n - 1) + solve(n - 3) + solve(n - 5);
}
// Memoized , complexity = O(n)
const int MAX=20;
//initialize all to 0
int result[MAX]={};
int solveMemoized(int n) {
if (n <0 ) return 0;
if (n ==0) return 1;
if (result[n]!=0) return result[n];
result[n] = solveMemoized(n-1)+solveMemoized(n-3) + solveMemoized(n-5);
return result[n];
}
int main() {
int num;
std::cout << " Enter number :";
std::cin >> num;
//std::cout << " Number of ways to form " << num << " is = " << solve(num);
std::cout << " Number of ways to form " << num << " is = " << solveMemoized(num);
}