Posts

Showing posts from November, 2025

Implementing Stack in JS

Revised Stack (LIFO) Data Structure: Revisited the Stack abstract data type in depth, including its core operations ( push , pop , peek , isEmpty , isFull ) and typical time/space complexities. Implemented stack logic from scratch using both arrays and linked lists, and explored how stacks are used in real scenarios like function call stacks, expression evaluation (infix → postfix), backtracking, undo/redo systems, and browser history navigation. Also practiced solving classic stack-based problems (balanced parentheses, Next Greater Element, and prefix/postfix evaluation) to strengthen implementation skills and problem-solving intuition. const prompt = require ( "prompt-sync" )() class Stack { constructor ( size ) { this . top = 0 this . size = size this . stack = new Array ( size ) .fill ( 0 ) } stackOverflow () { if ( this . top >= this . size ) { console .log ( "Stack Overflow error...

Solving : Jump Game ||, using Greedy Approch

Today I’m going to solve Jump Game II using a greedy approach . This solution runs in O(n) time and uses O(1) extra space . Instead of directly trying to count the jumps at every step, we focus on the maximum coverage (the farthest index we can currently reach). The idea is: in each “window” of indices that we can reach with the current jump, we try to extend our coverage as far as possible, so that the next jump takes us closer to the end. I struggled with this problem at first. The recursive solution is very slow, and more general methods are also time-consuming. The greedy approach, however, sets everything up in a very efficient and elegant way. To implement it, I use three main variables: totalJumps – counts how many jumps we have made so far. currentEnd (or destination ) – the end of the current coverage window; when we reach this index, we must make a jump. farthest – the farthest index we can reach from any index inside the current window. As we iterate throug...

Learning Recursion based Permutations

 I'm revising recursion again today. I've already learned it this way, but I need to revise it this way again. Yesterday I learned Grady Algorithms. I had to learn recursion again because this topic is a little difficult, but not impossible. It is quite true that backtracking is perfect for recursion-based permutations. I am learning this because I am trying to solve LeetCode question 46.  Finally, I found the solution: Pardon me if any misslogic, var permute = function ( nums ) { let ans = [] let i = 0 let n = nums . length - 1 getPerm ( nums , ans , i , n ) return ans }; let getPerm = ( nums , ans , idx , n ) => {          if(idx === n) {          ans.push([...ans])          return      }     for(let i=idx; i<n; i++) {                     let tmp = nums[id...