Fading Coder

One Final Commit for the Last Sprint

Dynamic Programming Practice: Fibonacci, Climbing Stairs, and Minimum Cost

Fibonacci Number (LeetCode 509) The Fibonacci seqeunce is defined as: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) for n > 1 Example: Input: n = 4 Output: 3 (Sequence: 0, 1, 1, 2, 3) Solution using constant space: class Solution: def fib(self, n: int) -> int: if n < 2: return n # Initialize...

Hash Table Fundamentals and Core Algorithmic Applications

Hash tables provide O(1) average-time complexity for insertion, deletion, and lookup operations. They are the optimal choice when the primary requirement is rapidly verifying the existence of an element within a collection or tracking frequency counts. The core mechanism relies on a hash function th...

LeetCode Weekly Problem Solutions Collection

8. Find Pivot Index Problem: Given an integer array nums, find the pivot index where the sum of elements to the left equals the sum of elements to the right. If no such index exists, return -1. Solution Approach: Single-pass traversal class Solution { public int findPivotIndex(int[] nums) { int tota...

Dynamic Programming Approach to Subsequence Problems on LeetCode

LeetCode 392: Is Subsequence Problem Description Given two strings s and t, determine if s is a subsequence of t. A subsequence is a sequence that can be derived from another sequence by deleting some or no characters without changing the order of the remaining characters. Dynamic Programming Soluti...

Mastering Dynamic Programming: Fibonacci Sequences and Staircase Variants

Solving dynamic programming problems systematically typically involves five key steps: Define the dp array (table) and the meaning of its indices. Determine the recurernce relation. Initialize the dp array correctly with base cases. Select the correct iteration order. Trace example values to verify...

Backtracking Algorithm for Combinations

Problem Description Given two integers n and k, return all possible comibnations of k numbers out of the range [1, n]. You may return the answer in any order. Example 1: Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] Example 2: Input: n = 1, k = 1 Output: [[1]] Constraints...

Swapping Nodes in Pairs in a Linked List

When solving this problem initially, many developers might skip using a dummy head node and handle the swap directly. Here’s how that works: Define two nodes firstNode and secondNode to represent the adjacent pair being swapped, plus a nextPairHead to store the starting point of the next unprocessed...

Converting Roman Numerals to Integers: Implementation and Comparison

Roman numerals utilize seven specific symbols: I, V, X, L, C, D, and M. Their corresponding values are: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Numbers are formed by combining these symbols, typically written from largest to smallest value from left to right. For instance, "XII"...

Stack and Queue Algorithms: RPN Evaluation, Sliding Window Maxima, and Frequency Heaps

Evaluate Reverse Polish Notation Reverse Polish Notation (RPN) expressions are evaluated using a stack data structure. When iterating through the expression tokens, operands are pushed onto the stack. Upon encountering an operator, the top two operands are popped, the operation is executed, and the...

Implementing Linked List Operations: Reversal, Pairwise Swapping, and Nth Node Removal

Implementing Linked List Operations: Reversal, Pairwise Swapping, and Nth Node Removal
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...