#include<iostream>
using namespace std;
int count(int m, int n) {
if (m == 1 || n == 1) {
return 1;
}
else {
return count(m, n - 1) + count(m - 1, n);
}
}
int main() {
int m,n;
cin >>m>> n;
cout << count(m, n)<<endl;
}
I did this question using recursion we can also use DP(dynamic programming) in this. The time complexity of above solution is exponential.
Explanation of recursion is that we can come to (m,n-1) and take a step down or I can come to (m-1,n) and can take a right and come to (m,n).
using namespace std;
int count(int m, int n) {
if (m == 1 || n == 1) {
return 1;
}
else {
return count(m, n - 1) + count(m - 1, n);
}
}
int main() {
int m,n;
cin >>m>> n;
cout << count(m, n)<<endl;
}
I did this question using recursion we can also use DP(dynamic programming) in this. The time complexity of above solution is exponential.
Explanation of recursion is that we can come to (m,n-1) and take a step down or I can come to (m-1,n) and can take a right and come to (m,n).
No comments:
Post a Comment