Skip to content

Monotonic Stack

  • when to use stack
  • when you have \(O(n^2)\) solution and the inner loop is dependent on j
    • then you can have better solution in \(O(n)\)
for(int i=0;i<n;i++){
    for(int j=0;j<i;j++){
        // n^2 code complexity
        // actually - n(n+1)/2 time complexity
        // so we can use stack here to make it better
    }
}

Questions