This repository contains my personal collection of LeetCode problem solutions implemented in Go (Golang). The solutions are organized by problem categories and include comprehensive documentation, test cases, and multiple approaches where applicable.
The repository is organized into the following categories:
- Best Time to Buy and Sell Stock - Two-pointer technique for maximum profit optimization
- Contains Duplicate - Duplicate detection in arrays
- Contains Nearby Duplicate - Proximity-based duplicate detection
- Count the Number of Incremovable Subarrays I - Finding incremovable subarrays in arrays
- Defuse the Bomb - Circular array manipulation (Sliding Window)
- Find Median of Two Sorted Arrays - Binary search on sorted arrays
- Fruits into Baskets II - Sliding window for collecting fruits from baskets
- Group Anagrams - String grouping and hash map techniques
- Longest Common Prefix - String prefix matching
- Longest Consecutive Sequence - Sequence detection with O(n) complexity
- Longest Harmonious Subsequence - Array analysis with sliding window
- Maximum Strong Pair XOR - XOR operations and pair analysis (Sliding Window)
- Product of Array Except Self - Array manipulation without division
- Three Sum - Two-pointer technique for triplet finding
- Top K Frequent Elements - Frequency analysis and sorting
- Trapping Rain Water - Two-pointer technique for water trapping optimization
- Two Sum II - Input Array Is Sorted - Two-pointer technique on sorted arrays
- Valid Anagram - Character frequency comparison
- Valid Sudoku - 2D array validation with complex rules
- Array Partition - Greedy algorithm for maximum sum of minimums
- Can Place Flowers - Greedy placement of flowers in a linear bed
- Largest Perimeter Triangle - Greedy selection of triangle sides for maximum perimeter
- Lemonade Change - Greedy change-making for lemonade transactions
- Arranging Coins - Finding staircase rows using binary search
- Binary Search - Classic O(log n) search in sorted arrays
- Count Pairs Whose Sum is Less Than Target - Counting valid pairs using binary search on sorted array
- Fair Candy Swap - Finding pair for equal candy distribution using binary search
- Find Minimum in Rotated Sorted Array - Binary search in rotated sorted array
- Find Smallest Letter Greater Than Target - Finding next greater letter using binary search
- Find Target Indices After Sorting Array - Finding all target indices after sorting using binary search
- Find the Distance Value Between Two Arrays - Using binary search to count elements with minimum distance
- First Bad Version - Finding first bad version using binary search
- Guess Number Higher or Lower - Binary search guessing game with feedback
- Koko Eating Bananas - Binary search for optimization problems
- Kth Missing Positive Number - Finding kth missing positive number using binary search
- Longest Subsequence With Limited Sum - Finding longest subsequence within sum limit using binary search
- Maximum Count of Positive Integer and Negative Integer - Finding maximum count using binary search
- Median of Two Sorted Arrays - Binary search approach to find median efficiently
- Minimum Common Value - Finding minimum common value between two sorted arrays using binary search
- Missing Number - Finding missing number using binary search or mathematical approach
- Search in Rotated Sorted Array - Modified binary search for rotated arrays
- Search Insert Position - Finding insertion position using binary search
- Special Array With X Elements Greater Than or Equal X - Finding special value using binary search
- Sqrt(x) - Computing square root using binary search
- Time Based Key-Value Store - Binary search for time-based data retrieval
- Valid Perfect Square - Checking if a number is a perfect square using binary search
- Substrings of Size Three with Distinct Characters - Fixed-size window for unique character substrings
- Two Sum - Hash map for O(1) lookups in pair sum problems
- Add Two Numbers - Linked list arithmetic operations
- Add Two Numbers - Linked list arithmetic operations
- Copy List with Random Pointer - Deep copying of complex linked structure
- Linked List Cycle - Floyd's cycle detection algorithm
- LRU Cache - Linked list and hashmap implementation for cache
- Merge Two Sorted Lists - Iterative linked list merging
- Remove Nth Node From End of List - Two-pointer technique for node removal
- Reorder List - Multiple linked list operations combined
- Reverse Linked List - In-place linked list reversal
- Convert Date to Binary - Date manipulation and binary conversion
- Greatest Common Divisor of Strings - String pattern analysis
- Check Valid Matrix - Matrix validation algorithms
- Search a 2D Matrix - Binary search in 2D matrix
- Car Fleet - Stack-based simulation of cars on a road
- Daily Temperatures - Monotonic stack for next greater element problems
- Evaluate Reverse Polish Notation - Stack-based expression evaluation with custom stack implementation
- Min Stack - Stack data structure with constant-time minimum retrieval
- Final Prices With a Special Discount in a Shop - Next smaller element pattern
- Largest Rectangle in Histogram - Stack-based area calculation with monotonic properties
- Next Greater Element I - Monotonic decreasing stack for next greater element
- Count K-Constraint Substrings - Sliding window technique for substring counting
- Generate Parentheses - Backtracking algorithm for combinatorial generation
- Valid Palindrome - Two-pointer technique with string preprocessing
- Valid Parentheses - Stack-based bracket matching
- Balanced Binary Tree - Tree balance validation
- Binary Tree Level Order Traversal - BFS approach for level-by-level tree traversal
- Binary Tree Right Side View - Modified BFS to view tree from right side
- Construct Binary Tree from Preorder and Inorder Traversal - Tree reconstruction from traversals
- Count Good Nodes in Binary Tree - DFS with path maximum tracking
- Diameter of Binary Tree - Maximum path length calculation
- Invert Binary Tree - Tree mirroring operation
- Kth Smallest Element in a BST - Efficient BST traversal for selection
- Lowest Common Ancestor of a Binary Search Tree - BST property-based traversal
- Maximum Depth of Binary Tree - Recursive tree height calculation
- Same Tree - Tree comparison and structural equality
- Subtree of Another Tree - Subtree matching algorithm
- Validate Binary Search Tree - BST property validation using inorder traversal
- Find the K-Beauty of a Number - Fixed-size sliding window over digits
- Longest Nice Substring - Recursive sliding window for substring property validation
- Longest Repeating Character Replacement - Sliding window with character frequency counting
- Longest Substring Without Repeating Characters - Dynamic sliding window technique
- Maximum Average Subarray I - Fixed-size sliding window with running sum
- Minimum Difference Between Highest and Lowest of K Scores - Fixed-size window after sorting
- Minimum Window Substring - Variable-size sliding window optimization
- Permutation in String - Fixed-size sliding window with pattern matching
- Assign Cookies - Greedy assignment using two pointers
- Check If N and Its Double Exist - Finding pairs where one number is double of another using two pointers
- Container With Most Water - Two-pointer technique for maximum area between lines
- Count Binary Substrings - Counting consecutive groups with two pointers
- DI String Match - Constructing permutation based on increase/decrease pattern using two pointers
- Duplicate Zeros - In-place array modification duplicating zeros using two pointers
- Find the Duplicate Number - Floyd's cycle detection in array form
- Find the Index of the First Occurrence in a String - String matching with two pointers
- Happy Number - Floyd's cycle detection for number sequences
- Intersection of Two Arrays - Finding common elements between arrays
- Intersection of Two Arrays II - Finding all common elements with duplicates
- Is Subsequence - Efficient subsequence validation with two pointers
- Long Pressed Name - Validating typed name with possible long key presses using two pointers
- Move Zeroes - In-place array rearrangement with two pointers
- Remove Duplicates from Sorted Array - In-place array modification with two pointers
- Remove Element - In-place removal with two-pointer approach
- Remove Palindromic Subsequences - Palindrome validation and removal analysis using two pointers
- Reverse String - In-place string reversal with two pointers
- Reverse Only Letters - Reverse only letters while keeping non-letter characters in place
- Reverse String II - Partial string reversal with two pointers
- Reverse Vowels of a String - Selective character reversal using two pointers
- Reverse Words in a String III - Reverse words within a string using two pointers
- Shortest Distance to a Character - Computing minimum distances with two-pass approach
- Sort Array by Parity - In-place partitioning with two pointers
- Sort Array by Parity II - Arranging array with even indices for even numbers and odd indices for odd numbers
- Squares of a Sorted Array - Building sorted squares array using two pointers from both ends
- Valid Palindrome II - Palindrome validation with one deletion allowed
- Go (Golang) - All solutions implemented with idiomatic Go code
- Comprehensive use of Go's built-in data structures and standard library
- Clean Code Practices: Well-documented functions with clear variable names
- Multiple Approaches: Many problems include both brute force and optimized solutions
- Time & Space Complexity: Comments indicating algorithmic complexity
- Test Cases: Comprehensive test suites using Go's testing framework
- Sliding Window: Multiple implementations showcasing different sliding window patterns
- Two Pointers: Efficient array traversal techniques
- Hash Maps: Fast lookup and frequency counting
- Dynamic Programming: Optimal substructure solutions
- Binary Search: Logarithmic search algorithms
- Stack & Queue: LIFO and FIFO data structure utilization
- Tree Traversal: DFS and BFS implementations
- Sorting Algorithms: Custom and built-in sorting techniques
The repository includes problems across all difficulty levels:
- Easy: Fundamental algorithmic concepts
- Medium: Intermediate problem-solving techniques
- Hard: Advanced algorithms and optimizations
This repository contains excellent examples of sliding window techniques:
- Fixed Size Window: Count Good Substrings
- Variable Size Window: Count K-Constraint Substrings
- Multiple Window Approaches: Maximum Strong Pair XOR
- O(n) Solutions: Longest Consecutive Sequence
- Space Optimization: Product of Array Except Self
- Early Termination: Various sliding window implementations
- Unit Tests: Comprehensive test coverage with table-driven tests
- Edge Cases: Boundary condition testing
- Performance Testing: Benchmarking for optimization validation
- Go 1.19+ installed on your system
- Basic understanding of Go syntax and data structures
-
Clone the repository:
git clone <repository-url> cd leetcode
-
Navigate to any problem directory:
cd Arrays/two-sum -
Run the solution:
go run main.go
-
Run tests (where available):
go test
Each problem can be built as a standalone executable:
go build main.go
./main| Data Structure | Count | Examples |
|---|---|---|
| Arrays | 19 | Best Time to Buy and Sell Stock, Contains Duplicate, Contains Nearby Duplicate, Count the Number of Incremovable Subarrays I, Defuse the Bomb, Find Median of Two Sorted Arrays, Fruits into Baskets II, Group Anagrams, Longest Common Prefix, Longest Consecutive Sequence, Longest Harmonious Subsequence, Maximum Strong Pair XOR, Product of Array Except Self, Three Sum, Top K Frequent Elements, Trapping Rain Water, Two Sum II - Input Array Is Sorted, Valid Anagram, Valid Sudoku |
| Binary Search | 23 | Arranging Coins, Binary Search, Count Pairs Whose Sum is Less Than Target, Fair Candy Swap, Find Minimum in Rotated Sorted Array, Find Smallest Letter Greater Than Target, Find Target Indices After Sorting Array, Find the Distance Value Between Two Arrays, First Bad Version, Guess Number Higher or Lower, Koko Eating Bananas, Kth Missing Positive Number, Longest Subsequence With Limited Sum, Maximum Count of Positive Integer and Negative Integer, Median of Two Sorted Arrays, Minimum Common Value, Missing Number, Search in Rotated Sorted Array, Search Insert Position, Special Array With X Elements Greater Than or Equal X, Sqrt(x), Time Based Key-Value Store, Valid Perfect Square |
| Greedy | 4 | Array Partition, Can Place Flowers, Largest Perimeter Triangle, Lemonade Change |
| Hash Tables | 2 | Substrings of Size Three with Distinct Characters, Two Sum |
| Linked Lists | 9 | Add Two Numbers (2 versions), Copy List with Random Pointer, Linked List Cycle, LRU Cache, Merge Two Sorted Lists, Remove Nth Node From End of List, Reorder List, Reverse Linked List |
| Math | 2 | Convert Date to Binary, Greatest Common Divisor of Strings |
| Matrix | 2 | Check Valid Matrix, Search a 2D Matrix |
| Monotonic Stack | 3 | Final Prices With a Special Discount in a Shop, Largest Rectangle in Histogram, Next Greater Element I |
| Sliding Window | 8 | Find the K-Beauty of a Number, Longest Nice Substring, Longest Repeating Character Replacement, Longest Substring Without Repeating Characters, Maximum Average Subarray I, Minimum Difference Between Highest and Lowest of K Scores, Minimum Window Substring, Permutation in String |
| Stacks | 4 | Car Fleet, Daily Temperatures, Evaluate Reverse Polish Notation, Min Stack |
| Strings | 4 | Count K-Constraint Substrings, Generate Parentheses, Valid Palindrome, Valid Parentheses |
| Trees | 13 | Balanced Binary Tree, Binary Tree Level Order Traversal, Binary Tree Right Side View, Construct Binary Tree from Preorder and Inorder Traversal, Count Good Nodes in Binary Tree, Diameter of Binary Tree, Invert Binary Tree, Kth Smallest Element in a BST, Lowest Common Ancestor of a Binary Search Tree, Maximum Depth of Binary Tree, Same Tree, Subtree of Another Tree, Validate Binary Search Tree |
| Two Pointers | 27 | Assign Cookies, Check If N and Its Double Exist, Container With Most Water, Count Binary Substrings, DI String Match, Duplicate Zeros, Find the Duplicate Number, Find the Index of the First Occurrence in a String, Happy Number, Intersection of Two Arrays, Intersection of Two Arrays II, Is Subsequence, Long Pressed Name, Move Zeroes, Remove Duplicates from Sorted Array, Remove Element, Remove Palindromic Subsequences, Reverse Only Letters, Reverse String, Reverse String II, Reverse Vowels of a String, Reverse Words in a String III, Shortest Distance to a Character, Sort Array by Parity, Sort Array by Parity II, Squares of a Sorted Array, Valid Palindrome II |
- Hash Map Lookups - Fast O(1) access patterns
- Two Pointers - Efficient array traversal
- Sliding Window - Substring and subarray problems
- Sorting + Binary Search - Optimized search operations
- Stack Operations - LIFO processing patterns
- Bit Manipulation - XOR operations for unique solutions
- Mathematical Optimization - GCD and modular arithmetic
- Tree Algorithms - Recursive depth calculations
- Dynamic Programming - Optimal subproblem solutions
Each problem directory typically contains:
main.go- Primary solution implementationREADME.md- Problem description and approach explanation*_test.go- Unit tests (where applicable)go.mod- Module definition (for problems requiring external dependencies)
This repository serves as:
- Algorithm Reference - Practical implementations of common algorithms
- Go Programming Examples - Idiomatic Go code patterns
- Interview Preparation - Solutions to commonly asked coding questions
- Performance Analysis - Examples of optimization techniques
While this is a personal practice repository, suggestions and improvements are welcome:
- Fork the repository
- Create a feature branch
- Add your improvements
- Submit a pull request
This repository is for educational purposes. All LeetCode problems are property of LeetCode LLC.
- Repository Owner: huypq02
- Current Branch: main
- Language: Go (Golang)
Happy Coding! 🚀
"The best way to learn algorithms is to implement them yourself." - Practice makes perfect!