sashomaga

Find maximal increasing sequence in an array

Mar 13th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function FindIncreasingSequence()
  2. {
  3.     var strArray = document.getElementById('pr4').value.split(',');
  4.     var intArray = new Array(strArray.length);
  5.     for (var i = 0; i < strArray.length; i++) {
  6.         intArray[i] = parseInt(strArray[i]);
  7.     }
  8.    
  9.     var score = 1;
  10.     var bestScore = 0;
  11.     var currentValue = intArray[0];
  12.     var result = intArray[0] + ',';
  13.     var bestResult;
  14.     for (var i = 1; i < intArray.length; i++) {
  15.         if (currentValue < intArray[i]) {
  16.             score++;
  17.             result += intArray[i] + ',';
  18.             currentValue = intArray[i];
  19.         }
  20.         else
  21.         {
  22.             score = 1;          
  23.             currentValue = intArray[i];
  24.             result = currentValue+',';
  25.         }
  26.         //check for best
  27.         if (score > bestScore) {
  28.             bestScore = score;
  29.             bestResult = result;
  30.         }
  31.     }  
  32.     //remove last comma
  33.     bestResult = bestResult.substring(0, bestResult.length - 1);
  34.     //print  
  35.     cons.innerHTML = "{" + bestResult + "}";
  36. }
Advertisement
Add Comment
Please, Sign In to add comment