Skip to content

Commit

Permalink
new question added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Jun 22, 2017
1 parent 612cf00 commit ee87380
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ is known.
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.
- Sometimes results of two DP solutions can be merged using some algo to find the final result.
- To breakdown any question to DP (recursive equation), follow the crux of the question and break
it down into a story and later generalize to form recursive equations
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
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
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.
- See if a DP is becoming a fibonacci series. For eg: in the stairs problem
total ways to reach the nth step f(n) = f(n-1) + f(n-2)
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
nothing but fibonacci series.
- 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.


# Topic0: Programming Questions

Expand Down Expand Up @@ -386,6 +394,7 @@ it down into a story and later generalize to form recursive equations
- [Given a sentence without spaces between words. Break the words in such a way that they are meaningful](/dynamic-programming/question19.c)
- [Partition problem](/dynamic-programming/question20.c)
- [Find the longest palindromic subsequence](/dynamic-programming/question21.c)
- [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)

## Some important concepts to solve algos better

Expand Down
59 changes: 59 additions & 0 deletions dynamic-programming/question22.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Given n-stairs, how many number of ways a person can climb to top
from bottom using 1 step or 2 steps
Naive approach:
Lets say the stairs are given as 1 2 3 4 5 6... n
Generate all the sub sequences of the numbers from 1 to n. Now examine if each subsequence is posible
or not.
Eg: 1 4 5 is not possible as user can only take a max of 2 steps.
but 1 2 3 4 5 is valid.
Therefore it will take another O(n) time to examine such subsequences
Time complexity: O(2^n)n //which is exponential
METHOD:
DP:
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
n-1 + number of way to reach n-2.
which is f(n)=f(n-1) + f(n-2)
Therefore is is nothing but fibonacci series
Time complexity: O(n)
Space complexity: O(n)
*/

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

int f[MAX];

int fib(int n){
if(f[n] == -1){
if(n < 2){
f[n] = 1;
}else{
f[n] = fib(n-1) + fib(n-2);
}
}
return f[n];
}

void initialize(int n){
int i;
for(i=0; i<=n;i++){
f[i] = -1;
}
}

int main(){
//lets say value of n is 20
int n = 9;
initialize(n);
printf("number of ways to reach the %dth step is %d\n", n,fib(n));

return 0;
}

1 change: 1 addition & 0 deletions nextquestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ TODO:
- DP solution extension from geeks for geeks for question17 and course sol as well
- program to find fibonacci number in logn time (geeks for geeks)
- DP question 19 to be done
- finding all palindromes in a given string

0 comments on commit ee87380

Please sign in to comment.