LeetCode Problem 206: Reverse Linked List Problem Description: Solution: Using a two-pointer approach with previous and current pointers, iteratively modify pointer directions. Pay atttention to the loop termination condition; return the previous pointer as the new head node. /** * Definition for si...
A common pattern in two-pointer algorithms involves iterating through a sequence while adjusting a second pointer to maintain a specific condition. for (int left = 0, right = 0; right < n; right++) { while (left < right && condition(left, right)) left++; // Process the current window }...
Reverse Polish Notation (RPN), or postfix notation, is evaluated using a stack data structure. The algorithm iterates through an array of tokens. When a numeric token is encountered, it is pushed onto the stack. Upon encountering an operator (+, -, *, /), the two most recent numbers are popped from...
13.1 Backtracking Algorithm The "backtracking algorithm" is a problem-solving method that relies on exhaustive search. Its core concept is to start from an initial state and use brute force to search for all possible solutions. When a correct solution is found, it is recorded. The process...
Greedy Interval Scheduling When selecting the maximum number of non-overlapping events from a given set, a greedy strategy based on finish times yields an optimal solution. Sorting intervals by their end points ensures that each selected event leaves the maximum possible remaining time for subsequen...
Quicksort Quicksort uses a divide-and-conquer approach by selecting a pivot element. let dataset = [1, 2, 5, 6, 3, 1, 4]; function quickSort(data) { if (data.length < 2) { return data; } let pivotIdx = Math.floor(data.length / 2); let pivotValue = data.splice(pivotIdx, 1)[0]; let smaller = []; le...
In standard 0/1 knapsack problems, the objective is often to determine the number of ways to fill a specific capacity by adding items. However, challlenges arise when we need to compute the number of valid combinations after excluding a specific item from an already calculated set. Let $F[j]$ repres...
Sorting Algorithms Bubble Sort A simple comparison-based algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. function bubbleSort(sequence) { const length = sequence.length; for (let pass = 0; pass < length - 1; pass++) { for...
Efficient computation of $a^b \bmod m$ utilizes the binary representation of the exponent $b$. By expressing $b$ as $\sum_{i=0}^{k} c_i \cdot 2^i$ where $c_i \in {0,1}$, the power decomposes into a product of squared terms: $a^b = \prod_{i=0}^{k} (a^{2^i})^{c_i}$. The algorithm iterates through each...
Introduction to Data Structures A data structure fundamentally consists of two components: data and structure. Data encompasses all information processed by computers—numeric values, user records (names, ages, profiles), or multimedia content (text, images, videos). Structure defines how this data i...