JSFiddle - React, Tailwind, and code Playground

by Krishna Ananthi

JavaScript

/*function   lengthOfLIS(nums) {
        const LIS = new Array(nums.length).fill(1);

        for (let i = nums.length - 1; i >= 0; i--) {
            for (let j = i + 1; j < nums.length; j++) {
            console.log(LIS)
                if (nums[i] < nums[j]) {
                    LIS[i] = Math.max(LIS[i], 1 + LIS[j]);
                }
            }
        }
        return Math.max(...LIS);
    }*/
 function   lengthOfLIS(nums) {   
    const n = nums.length;
    const decksLowest = [];

    for (let num of nums) {
        let left = 0, right = decksLowest.length - 1;
        while (left <= right) {
            let mid = Math.floor((left + right) / 2);
            if (decksLowest[mid] < num) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }        
        console.log('**',left, decksLowest.length, decksLowest)
        if (left < decksLowest.length) {
            decksLowest[left] = num;
        } else {
            decksLowest.push(num);
        }
        console.log(decksLowest)
    }
    return decksLowest.length;
}

   // console.log(lengthOfLIS([10,2,4,3,6,-1]))
   
   function longestCommonSubsequence(text1, text2) {
        if(text1.length < text2.length)
         [text1,text2] = [text2,text1]; // text1 is longest

         let dp = new Array(text2.length+1).fill(0);

         for(let i= text1.length-1;i>=0;i--){
            let prev = 0
            for(let j=text2.length-1;j>=0;j--){
                let temp = dp[j];
                if(text1[i] === text2[j]){
                    dp[j] = 1+prev;
                }else{
                    dp[j] = Math.max(dp[j], dp[j+1])
                }
                
                console.log('**',dp, prev,temp)
                prev= temp;
            }
         }  
         return dp[0];
    }
    
  //  console.log(longestCommonSubsequence("cabrt","cat"))
    
    function canJump(nums) {
        for(let i=0;i<nums.length;){
           let newTarget...