Skip to content

Commit ee87380

Browse files
committed
new question added
1 parent 612cf00 commit ee87380

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ is known.
168168
linear array, array of length 1 less than total can be taken and a pattern can be found for dynamic programming to make recursive equations.
169169
- Sometimes results of two DP solutions can be merged using some algo to find the final result.
170170
- To breakdown any question to DP (recursive equation), follow the crux of the question and break
171-
it down into a story and later generalize to form recursive equations
171+
it down into a story and later generalize to form recursive equations. For eg: in case of finding the longest palindrome subsequence in a given string, we compare the last two elements to see if they match or not, this is something that we also do in a normal palindrome question. Here on
172+
basis of that we are able to derive to equations if they match we move both pointers to next location, if they dont we move one of them (two cases) and find whichever is maximum. Then
173+
using these recursive equations we build a tree taking an example in mind. Then we find overlapping problems. To see unique problems we generalize the question for which we made the tree to i and j and rather finding how many values of j can exist for each value of i everytime. By that calculation we find unique solutions and solve the question making a table of that size.
174+
- See if a DP is becoming a fibonacci series. For eg: in the stairs problem
175+
total ways to reach the nth step f(n) = f(n-1) + f(n-2)
176+
i.e from n-1 it can take you 1 step and from n-2 it can take you only 1 step of size 2. Therefore, this is
177+
nothing but fibonacci series.
178+
- In some algos involving DP you can start from n and in that case answer to n is dependent on n-1 and so on.
179+
172180

173181
# Topic0: Programming Questions
174182

@@ -386,6 +394,7 @@ it down into a story and later generalize to form recursive equations
386394
- [Given a sentence without spaces between words. Break the words in such a way that they are meaningful](/dynamic-programming/question19.c)
387395
- [Partition problem](/dynamic-programming/question20.c)
388396
- [Find the longest palindromic subsequence](/dynamic-programming/question21.c)
397+
- [Given n-stairs, how many number of ways a person can climb to top from bottom using 1 step or 2 steps](/dynamic-programming/question22.c)
389398

390399
## Some important concepts to solve algos better
391400

dynamic-programming/question22.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Given n-stairs, how many number of ways a person can climb to top
3+
from bottom using 1 step or 2 steps
4+
5+
Naive approach:
6+
Lets say the stairs are given as 1 2 3 4 5 6... n
7+
Generate all the sub sequences of the numbers from 1 to n. Now examine if each subsequence is posible
8+
or not.
9+
Eg: 1 4 5 is not possible as user can only take a max of 2 steps.
10+
but 1 2 3 4 5 is valid.
11+
Therefore it will take another O(n) time to examine such subsequences
12+
13+
Time complexity: O(2^n)n //which is exponential
14+
15+
METHOD:
16+
DP:
17+
Here if we start from number of ways it will take to reach nth step, it will be equal to number of ways to reach
18+
n-1 + number of way to reach n-2.
19+
which is f(n)=f(n-1) + f(n-2)
20+
Therefore is is nothing but fibonacci series
21+
22+
Time complexity: O(n)
23+
Space complexity: O(n)
24+
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#define MAX 100
30+
31+
int f[MAX];
32+
33+
int fib(int n){
34+
if(f[n] == -1){
35+
if(n < 2){
36+
f[n] = 1;
37+
}else{
38+
f[n] = fib(n-1) + fib(n-2);
39+
}
40+
}
41+
return f[n];
42+
}
43+
44+
void initialize(int n){
45+
int i;
46+
for(i=0; i<=n;i++){
47+
f[i] = -1;
48+
}
49+
}
50+
51+
int main(){
52+
//lets say value of n is 20
53+
int n = 9;
54+
initialize(n);
55+
printf("number of ways to reach the %dth step is %d\n", n,fib(n));
56+
57+
return 0;
58+
}
59+

nextquestions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ TODO:
8787
- DP solution extension from geeks for geeks for question17 and course sol as well
8888
- program to find fibonacci number in logn time (geeks for geeks)
8989
- DP question 19 to be done
90+
- finding all palindromes in a given string
9091

0 commit comments

Comments
 (0)