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 15, 2017
1 parent 5da0677 commit 25acfe1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ sometimes, multiple variables can be used.
is known.
- For problems involving subsequences, we can break them down to smaller problems. Specifically for
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.

# Topic0: Programming Questions

Expand Down Expand Up @@ -377,6 +377,7 @@ linear array, array of length 1 less than total can be taken and a pattern can b
- [Find kth ugly number](/dynamic-programming/question12.c)
- [Find the longest increasing subsequence](/dynamic-programming/question13.c)
- [Find the longest decreasing subsequence](/dynamic-programming/question14.c)
- [Perfect hill longest subsequence](/dynamic-programming/question15.c)


## Some important concepts to solve algos better
Expand Down
Binary file modified dynamic-programming/a.exe
Binary file not shown.
93 changes: 93 additions & 0 deletions dynamic-programming/question15.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Perfect hill longest subsequence
Longest subsequence of length 2k+1 where the first k+1 are strictly increasing, and last k+1 are strictly
decreasing.
METHOD:
Here we need to find the longest increasing sub sequence for the given array as did earlier.
We will store it in an array. In a separate array we will find longest decreasing sub sequence.
Then we will traverse both of these arrays to find the longest perfect hill sequence.
If for a number longest increasing sequence (from left) is 3 and longest decreasing sequence
towards right is 5. that means for a perfect hill sequence numbers need to be balanced on both the sides.
There we take the min of the two and subtract 1. Multiply the number with 2 and add 1. That will be the
size of perfect hill sequence. The maximum one will be the answer
Time complexity: O(n^2) + O(n) == O(n^2) only
//to compute LIS and LDS, then its just traversing once
Space complexity: O(n) //to store the arrays
*/
#include <stdio.h>
#include <stdlib.h>

int findMin(int a, int b){
return (a<b)?a:b;
}

void findLis(int *arr, int *lis, int size){
int i,j;
lis[0] = 1;
for(i=1;i<size;i++){
int max = 1;
for(j=i-1;j>=0;j--){
int key = 1;
if(arr[i] > arr[j]){
key = key + lis[j];
if(max < key){
max = key;
}
}
}
lis[i] = max;
}
}

void findLds(int *arr, int *lds, int size){
int i,j;
lds[size-1] = 1;
for(i=size-2;i>=0;i--){
int max = 1;
for(j=i+1;j<size;j++){
int key = 1;
if(arr[i] > arr[j]){
key = key + lds[j];
if(max < key){
max = key;
}
}
}
lds[i] = max;
}
}

int computePHS(int *arr, int *lis, int *lds, int size){

findLis(arr,lis,size);
findLds(arr,lds,size);

int i;
int max = 1;
for(i=0;i<size;i++){
int len = (findMin(lis[i],lds[i])-1)*2 + 1;
if(max < len){
max = len;
}
}
return max;
}

int main(){
int arr[] = {10,15,16,9,4,3,11,1};
int size = sizeof(arr)/sizeof(arr[0]);

int *lis = (int *)malloc(sizeof(int)*size);
int *lds = (int *)malloc(sizeof(int)*size);

int max = computePHS(arr,lis, lds,size);

printf("max length of perfect hill sequence is %d\n", max);

return 0;
}
3 changes: 2 additions & 1 deletion nextquestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ TODO:
- DP question6 to be done
- DP question7 to be done
- DP question11 (method1 naive) to be done
- DP question13 and 14(printing the subsequence to be done)
- DP question13 and 14(printing the subsequence to be done)
- https://gsamaras.wordpress.com/code/caution-when-reading-char-with-scanf-c/

0 comments on commit 25acfe1

Please sign in to comment.