Skip to content

huypq02/leetcode

Repository files navigation

LeetCode Solutions in Go

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.

📁 Repository Structure

The repository is organized into the following categories:

🔢 Arrays

💰 Greedy

🔍 Binary Search

🗂️ Hash Table

🔗 Linked List

🧮 Math

📊 Matrix

📚 Stack

📉 Monotonic Stack

🔤 String

🌳 Tree

🔍 Sliding Window

↔️ Two Pointers

🛠️ Technical Features

Programming Language

  • Go (Golang) - All solutions implemented with idiomatic Go code
  • Comprehensive use of Go's built-in data structures and standard library

Code Quality

  • 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

Algorithmic Techniques

  • 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

📈 Difficulty Levels

The repository includes problems across all difficulty levels:

  • Easy: Fundamental algorithmic concepts
  • Medium: Intermediate problem-solving techniques
  • Hard: Advanced algorithms and optimizations

🔍 Special Features

Sliding Window Problems

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

Optimization Examples

  • O(n) Solutions: Longest Consecutive Sequence
  • Space Optimization: Product of Array Except Self
  • Early Termination: Various sliding window implementations

Testing

  • Unit Tests: Comprehensive test coverage with table-driven tests
  • Edge Cases: Boundary condition testing
  • Performance Testing: Benchmarking for optimization validation

🚀 Getting Started

Prerequisites

  • Go 1.19+ installed on your system
  • Basic understanding of Go syntax and data structures

Running Solutions

  1. Clone the repository:

    git clone <repository-url>
    cd leetcode
  2. Navigate to any problem directory:

    cd Arrays/two-sum
  3. Run the solution:

    go run main.go
  4. Run tests (where available):

    go test

Building Solutions

Each problem can be built as a standalone executable:

go build main.go
./main

📊 Problem Categories by Data Structure

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

🎯 Algorithm Patterns

Most Common Patterns

  1. Hash Map Lookups - Fast O(1) access patterns
  2. Two Pointers - Efficient array traversal
  3. Sliding Window - Substring and subarray problems
  4. Sorting + Binary Search - Optimized search operations
  5. Stack Operations - LIFO processing patterns

Advanced Techniques

  • Bit Manipulation - XOR operations for unique solutions
  • Mathematical Optimization - GCD and modular arithmetic
  • Tree Algorithms - Recursive depth calculations
  • Dynamic Programming - Optimal subproblem solutions

🔧 Code Organization

Each problem directory typically contains:

  • main.go - Primary solution implementation
  • README.md - Problem description and approach explanation
  • *_test.go - Unit tests (where applicable)
  • go.mod - Module definition (for problems requiring external dependencies)

📚 Learning Resources

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

🤝 Contributing

While this is a personal practice repository, suggestions and improvements are welcome:

  1. Fork the repository
  2. Create a feature branch
  3. Add your improvements
  4. Submit a pull request

📄 License

This repository is for educational purposes. All LeetCode problems are property of LeetCode LLC.

📞 Contact

  • 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!

About

DSA - Focused on solving Data Structures and Algorithms (DSA) problems on the LeetCode platform to reinforce conceptual understanding and improve computational problem-solving performance.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages