Skip to main content

Posts

Showing posts from 2019

Longest Common Substring using recursion, memorization and dynamic programming

working example https://stackblitz.com/edit/js-isyptv?file=index.js running code //Longest Common Substring using recursion 1 let  r1 = 0 ; let  m1 = 0 ; String . prototype . subStringR = function ( str2 , r = this . length , c = this . length , count = 0 ) {      r1 += 1 ;      if ( r === 0   ||  c === 0 )      {        return  count ;      }        if ( this [ r - 1 ]=== str2 [ c - 1 ])      {       count = this . subStringR ( str2 , r - 1 , c - 1 , count + 1 );      }            count = Math . max ( count , this . subStringR ( str2 , r - 1 , c , 0 ), this . subStringR ( str2 , r , c - 1 , 0 ));        retur...

Longest Common Subsequence | Introduction & LCS Length

Longest Common Subsequence | Introduction & LCS Length all three solution. 1. first one is recursion 2. second one is  Memorization using top down approach     3. dynamic programming using a 2d matrix filling from top using bottom up approach  complete example is here https://stackblitz.com/edit/js-3rmcpu really nice video https://www.youtube.com/watch?v=sSno9rV8Rhg //longest comman subsequence  //watch this video for more detail //https://www.youtube.com/watch?v=sSno9rV8Rhg let  r = 0 ; //check how many call happen using Recursion let  m = 0 ; //check how many call happen using Memoization top down appoarch  let  d = 0 ; //check how many call happen using dynamic programming bottom up appoarch //using Recursion St...

Using the Effect Hook complete example how effect hook works in js

working link ======== https://stackblitz.com/edit/useeffect-hook?file=index.js Similar to componentDidMount and componentDidUpdate (class component) //code  const getUser=async()=>{         let response = await fetch(           props.url);                 let user = await response.json();              drawView(user)           }   //just one call like  componentDidMount     useEffect(()=>{             getUser();                },[])  //update on every render      useEffect(()=>{  ...

randomize all elements in a array inplace

randomize all elements in an array (in-place algorithms) take no space and its fastest among all random algorithms. //fastest algo to randomize array values without any space inplace   Array . prototype . swap = function ( startIndex , endIndex = this . length - 1 ) {     let  temp = this [ startIndex ];     this [ startIndex ]= this [ endIndex ];     this [ endIndex ]= temp ;     return   this ; }; Array . prototype . random = function () {      const  randomFunction =( min , max )=>{          return   Math . round ( Math . random ()*( max - min ))+ min ;      }      for ( let  i = 0 ; i < this . length ; i ++)      {         this . swap ( i , randomFuncti...

some inplace algorithms which can be used to swap two values based on index either based on values or based on index and value

1. swap two values based on the index i think this is the fastest algorithms but if you have something else in your book please share with me. 2. second algorithms swap two values in an array its quite different than first its swap two values. 3. third one is quite highly used its swaps a value and a index. check out the running work https://stackblitz.com/edit/js-fn31dt //swap two values in array based on index in place algo Array . prototype . swap = function ( startIndex , endIndex = this . length - 1 ) {     let  temp = this [ startIndex ];     this [ startIndex ]= this [ endIndex ];     this [ endIndex ]= temp ;     return   this ; }; console . log ([ 1 , 2 , 3 , 4 , 5 ]. swap ( 2 , 3 )) //swap two values by value required swap function in place algo Array . prototype . s...

Object pooling in game using a small coding example c++

When its comes to gaming either you are using unity or cocos2dx or unreal or godot or any other engine you need a object pool specially when you dealing with mobile and low hardware device because they have memory constraint. lets take a example of endless game where there is 100 of types of object that moves from either left to right or in 3d far to close using camera so one idea is that you can create graphic at runtime and remove its when its out of the screen but every time a new graphic created a new memory allocation happen and creating two many graphics at runtime can reduce frame rate and increase memory usage, so its deal with it 1. create a object pool for example a c++ example where bullet class and boom class can used same pool class and we can create two separate pool and each pool can hold 100 bombs and 100 bullet so when bomb or bullet out of the screen we can send it back to the pool. 2. so you are holding 200 instance at the same time and when they are not in ...

Context is one is most of important concept of react take a look

https://reactjs.org/docs/context.html Context is one of the most important part of react framework Context provide a api to pass global properties in a react dom tree and commonly used where you need to pass similar properties between components. 1. example passing laungugae or currency sign between component. 2. passing same theme across nested components 3. passing global properties across nested component. working exmaple https://stackblitz.com/edit/react-hmqbvz?file=index.js

hooks are new concept of react i am trying to use useState hooks to simplify my problem.

Read more about hooks here https://reactjs.org/docs/hooks-intro.html here is a example of hooks where i tried to create three hooks https://stackblitz.com/edit/react-1tprrk a real simple hook import   React ,   {  useState  }   from   'react' ; //my counter class using useState export   const   Counter =( props )=>{      //each function component can handle in own state      const   [ count , setCount ]= useState ( 0 );         return (          < div style ={{ background : props . color }}> Your  click  Button  is:<button onClick ={            ()=>{             setCount (++ count );    ...

Merge sort algorithm in a classic way example contain c++ code using vector

Merge sort  one of the highly used algorithm for sorting data check out this video for more detail https://www.youtube.com/watch?v=TzeBrDU-JaY Benefits over other sorting algorithms for  faster than bubble sort, insertion sort and selection sort. always used when time complexity is your first priority over space complexity. when data is huge 1000+ records but space complexity is not a problem. huge unsorted data means lots of records is unsorted. don't use for small range where data is already sorted or only a few records needed to sort use selection sort in this case. take's O(n) space because its a divide a conquer algorithm. running code just copy and paste and run it. // #include <iostream> #include <vector> #include <list> using namespace std ; //@why merge sort //faster than bubble sort //faster than insertion sort //KeyNote //divide recursively array/vector into smaller part...