Guest User

Untitled

a guest
Nov 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /**
  2. * LIS = Longest increasing subsequence.
  3. * Input = [10, 22, 9, 33, 21, 50, 41, 60, 80]
  4. * Output = [10, 22, 33, 50, 60, 80]
  5. * Created by gaurav on 1/7/15.
  6. */
  7.  
  8.  
  9. function findSubsequence(arr){
  10. var allSubsequence = [],
  11. longestSubsequence = null,
  12. longestSubsequenceLength = -1;
  13.  
  14. for(var i=0;i<arr.length;i++){ //i=1
  15. var subsequenceForCurrent = [arr[i]],
  16. current = arr[i],
  17. lastElementAdded = -1;
  18. for(var j=i;j<arr.length;j++){
  19. var subsequent = arr[j];
  20. if((subsequent > current) && (lastElementAdded<subsequent)){
  21. subsequenceForCurrent.push(subsequent);
  22. lastElementAdded = subsequent;
  23. }
  24. }
  25. allSubsequence.push(subsequenceForCurrent);
  26. }
  27. for(var i in allSubsequence){
  28. var subs = allSubsequence[i];
  29. if(subs.length>longestSubsequenceLength){
  30. longestSubsequenceLength = subs.length;
  31. longestSubsequence = subs;
  32. }
  33. }
  34. return longestSubsequence;
  35. }
  36.  
  37.  
  38. (function driver(){
  39. var sample = [87,88,91, 10, 22, 9,92, 94, 33, 21, 50, 41, 60, 80];
  40. console.log(findSubsequence(sample));
  41. })();
  42.  
  43. lastElementAdded = -1;
  44.  
  45. [ 87, 88, 91, 92, 94 ]
  46. [ 88, 91, 92, 94 ]
  47. [ 91, 92, 94 ]
  48.  
  49. function findLongestIncreasingSequence(array) {
  50. var sequence = [],
  51. fork = null;
  52.  
  53. // Always add the first value to the sequence
  54. sequence.push(array[0]);
  55.  
  56. // Reduce the array with. Since no initial accumulator is given,
  57. // the first value in the array is used
  58. array.reduce(function (previous, current, index) {
  59.  
  60. // If the current value is larger than the last, add it to the
  61. // sequence and return (i.e. check the next value)
  62. if(current > previous) {
  63. sequence.push(current);
  64. return current;
  65. }
  66.  
  67. // If, however, the value is smaller, and we haven't had a fork
  68. // before, make one now, starting at the current value's index
  69. if(!fork && current < previous) {
  70. fork = findLongestIncreasingSequence(array.slice(index));
  71. }
  72.  
  73. // Return the previous value if the current one is less or equal
  74. return previous;
  75. });
  76.  
  77. // Compare the current sequence's length to the fork's (if any) and
  78. // return whichever one is larger
  79. return fork && fork.length > sequence.length ? fork : sequence;
  80. }
  81.  
  82. findLongestIncreasingSequence(sample); // => [ 87, 88, 91, 92, 94 ]
  83.  
  84. Initial input: 87 88 91 10 22 9 92 94 33 21 50 41 60 80
  85. ====================================================================
  86. 1st call: √ √ √ F . . √ √ . . . . . .
  87. 2nd call: √ √ F √ √ . . . . . .
  88. 3rd call: √ √ √ F . . . . .
  89. 4th call: √ F √ . √ √
  90. 5th call: √ √ F √ √
  91. 6th call: √ √ √
  92.  
  93. let max_ref=1;
  94.  
  95. function longestIncreasingSubsequence(arr,n) {
  96.  
  97. if (n == 1)
  98. return 1;
  99. var res, max_ending_here = 1;
  100.  
  101. for (var i = 1; i < n; i++)
  102. {
  103. res = longestIncreasingSubsequence(arr,i);
  104. if (arr[i-1] < arr[n-1] && res + 1 > max_ending_here)
  105. max_ending_here = res + 1;
  106. }
  107. if (max_ref < max_ending_here)
  108. max_ref = max_ending_here;
  109.  
  110. return max_ending_here;
  111. }
  112.  
  113. let result = longestIncreasingSubsequence(arr,arr.length);
Add Comment
Please, Sign In to add comment