CS Theory

Stock Trading Problems: From One Deal to K Deals

Solutions to the classic stock trading series on LeetCode. Start with one transaction maximum, then unlimited trades, then two transactions, and finally k transactions. Each builds on dynamic programming concepts.

February 5, 2026

Stone Game

A dynamic programming solution to the classic Stone Game problem where two players take turns picking stones from either end of an array. The first player always wins when both play optimally.

February 5, 2026

String Rotation and Reversal Techniques

Practical techniques for string rotation and reversal operations including three-step reversal, in-place word reversal, and efficient rotation detection. Covers time and space complexity analysis with C++ implementations.

February 5, 2026

The Dutch National Flag Problem

Solve the classic sorting problem with three colors using a simple three-pointer technique. This elegant solution mirrors quicksort partitioning and runs in linear time with constant space.

February 5, 2026

The Kth Permutation Sequence: Two Approaches

Two solutions for finding the kth lexicographic permutation of numbers 1 to n. The first uses STL next_permutation with O(n!) time complexity. The second applies mathematical insight about factorial positions to achieve O(n) time complexity.

February 5, 2026

Top K Frequent Elements: Three Approaches

Solving the classic Top K Frequent Elements problem with bucket sort O(n) and max heap O(n log n). Bucket sort maps frequencies directly to indices while heap maintains top elements efficiently.

February 5, 2026

Two City Scheduling: A Dynamic Programming Solution

Given 2N people to split between two cities with different assignment costs, find the minimum total cost. This classic problem uses dynamic programming with state tracking to handle the constraint that exactly N people must go to each city.

February 5, 2026

Unique Binary Search Trees and Catalan Numbers

How to count unique binary search trees using dynamic programming. The solution connects to Catalan numbers and appears in various combinatorial problems like parentheses matching and stack operations.

February 5, 2026

Unique Binary Search Trees II

Generate all structurally unique binary search trees containing nodes 1 to N using recursive divide-and-conquer approach.

February 5, 2026

Validate Binary Search Trees

Check if a binary tree is a valid BST using in-order traversal. Time complexity O(logN) average for tree height or O(N) worst case. Space complexity O(logN) for stack and previous node tracking.

February 5, 2026