forked from amantyagi22/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtileProblemII.cpp
More file actions
36 lines (32 loc) · 761 Bytes
/
tileProblemII.cpp
File metadata and controls
36 lines (32 loc) · 761 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
35
36
#include<iostream>
using namespace std;
//Top down approach
int tiles(int n,int m,int* dp){
if(n==0) return 0;
if(n>0 && n<m) return 1;
if(n==m) return 2;
if(dp[n]!=0){
return dp[n];
}
int x = tiles(n-1,m,dp);
int y = tiles(n-m,m,dp);
return dp[n] = x+y;
}
//Bottom Up approach
int tilesdp(int n,int m){
int dp[100] = {0};
for(int i=0;i<=n;i++){
if(i==0) dp[i] = 0;
else if(i>0 && i<m) dp[i] = 1;
else if(i==m) dp[i] = 2;
else dp[i] = dp[i-1] + dp[i-m];
}
return dp[n];
}
int main(){
int n,m; cin >> n >> m;
int dp[100] = {0};
cout << tiles(n,m,dp) << endl;
cout << tilesdp(n,m) << endl;
}
//The block given is nxm and tile is 1xm or mx1