Fork of github.com/TomPlanche/leetcode
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-05-09 00:26:31 +02:00
leetcode_cli [157] (feat on main) Better error handling + added problem 38. 2025-04-19 05:43:09 +02:00
problems [166] (feat on main) Added problems 3341 and 3342. 2025-05-09 00:26:31 +02:00
update_readme [163] (feat on main) Added problems 2071 and 2962 + new april 2025 badge. 2025-05-01 02:17:04 +02:00
.gitignore [62] (feat on main) Added better .gitignore. 2024-12-04 15:22:27 +01:00
LICENCE [57] (feat on main) Better README generation + license file. 2024-12-03 13:46:53 +01:00
README.md [166] (feat on main) Added problems 3341 and 3342. 2025-05-09 00:26:31 +02:00

LeetCode Solutions in Rust 🦀

A collection of LeetCode problem solutions implemented in Rust, with a focus on clean code and detailed explanations.

Solutions Top Language License
Leetcode 50 days badge 2024 Leetcode 50 days badge 2025 Leetcode 100 days badge 2025 Leetcode November 2024 badge Leetcode December 2024 badge Leetcode January 2025 badge Leetcode February 2025 badge Leetcode March 2025 badge Leetcode April 2025 badge

📊 Progress

  • Total Solved: 232 problems
    • Easy: 43 (18.5%) 🟢
    • Medium: 151 (65.1%) 🟡
    • Hard: 38 (16.4%) 🔴

📁 Repository Structure

leetcode/
├── problems/
│   └── id_*/ (Solution directories)
│       ├── src/
│       │   └── main.rs (Solution implementation)
│       └── Cargo.toml
├── leetcode_cli/ (Project creation tool)
└── update_readme/ (README update script)

🛠️ Tools & Utilities

LeetCode CLI

A command-line tool for quickly creating new solution projects:

# todays
new_leetcode --daily

# specific id
new_leetcode 1234 --difficulty Medium --tags "array,dp" --title "Problem Title"

Learn more about LeetCode CLI

Automatic README Updates

This repository uses a pre-commit hook to automatically update the README.md file whenever changes are committed. The system includes:

  1. update_readme - A Rust script that:

    • Scans the problems directory
    • Extracts solution metadata
    • Updates statistics
    • Generates the solutions table
    • Updates the README.md file
  2. Pre-commit Hook Setup

    • Add the following script to .git/hooks/pre-commit:
    #!/usr/bin/env bash
    # run the `./update_readme/target/release/update_readme` binary to update the README.md
    # when files in `./problems/id_*` are changed.
    
    # get the list of files that have been changed
    # since the last commit
    files=$(git diff --cached --name-only)
    
    # check if any of the files are in the `./problems/id_*` directory
    if [[ $files == *"problems/id_"* ]]; then
        # if so, run the `update_readme` binary
        ./update_readme/target/release/update_readme
        # add the changes to the commit
        git add README.md
    fi
    
    # continue with the commit
    exit 0
    

    Make sure to give the script execution permissions:

    chmod +x .git/hooks/pre-commit
    

The script will run automatically before each commit, ensuring the README is always up to date with:

  • Current solution count and statistics
  • Complete solutions table
  • Difficulty distribution

📝 Solution Format

Each solution includes this header format for easy navigation and reference (and for the readme generator to parse):

//!
//! # Problem Title (Difficulty) [Tag1, Tag2]
//! LeetCode Problem {id}
//!

📋 Solutions Table

ID Title Difficulty Tags
1 Two sums Easy Array, Hash Table
7 Reverse Integer Medium Math
9 Palindrome Number Easy Math
10 Regular Expression Matching Hard String, Dynamic Programming, Recursion
12 Integer to Roman Medium Hash Table, Math, String
20 Valid Parentheses Easy String, Stack
22 Generate Parentheses Medium String, Dynamic Programming, Backtracking
29 Divide Two Integers Medium Math, Bit Manipulation
37 Sudoku Solver Hard Array, Hash Table, Backtracking, Matrix
38 Count and Say Medium String
239 Sliding Window Maximum Hard Array, Queue, Sliding Window, Heap (Priority Queue), Monotonic Queue
273 Integer to English Words Hard Math, String, Recursion
368 Largest Divisible Subset Medium Array, Math, Dynamic Programming, Sorting
407 Trapping Rain Water II Hard Array, Breadth First Search, Heap (priority Queue), Matrix
416 Partition Equal Subset Sum Medium Array, Dynamic Programming
494 Target Sum Medium Array, Dynamic Programming, Backtracking
515 Find Largest Value in Each Tree Row Medium Tree, Depth First Search, Breadth First Search, Binary Tree
567 Permutation in String Medium Hash Table, Two Pointers, String, Sliding Window
670 Maximum Swap Medium Math, Greedy
684 Redundant Connection Medium Depth First Search, Breadth First Search, Union Find, Graph
689 Maximum Sum of 3 Non-Overlapping Subarrays Hard Array, Dynamic Programming
729 My Calendar I Medium Array, Binary Search, Design, Segment Tree, Ordered Set
763 Partition Labels Medium Hash Table, Two Pointers, String, Greedy
769 Max Chunks To Make Sorted Medium Array, Stack, Greedy, Sorting, Monotonic Stack
773 Sliding Puzzle Hard Array, Breadth First Search, Matrix
781 Rabbits in Forest Medium Array, Hash Table, Math, Greedy
790 Domino and Tromino Tiling Medium Dynamic Programming
796 Rotate String Easy String, String Matching
802 Find Eventual Safe States Medium Depth First Search, Breadth First Search, Graph, Topological Sort
827 Making A Large Island Hard Array, Depth First Search, Breadth First Search, Union Find, Matrix
838 Push Dominoes Medium Two Pointers, String, Dynamic Programming
862 Shortest Subarray with Sum at Least K Hard Array, Binary Search, Queue, Sliding Window, Heap (Priority Queue), Prefix Sum, Monotonic Queue
873 Length of Longest Fibonacci Subsequence Medium Array, Hash Table, Dynamic Programming
889 Construct Binary Tree from Preorder and Postorder Traversal Medium Array, Hash Table, Divide And Conquer, Tree, Binary Tree
916 Word Subsets Medium Array, Hash Table, String
921 Minimum Add to Make Parentheses Valid Medium String, Stack, Greedy
951 Minimum Add to Make Parentheses Valid Medium String, Stack, Greedy
962 Maximum Width Ramp Medium Array, Stack, Monotonic Stack
983 Minimum Cost For Tickets Medium Array, Dynamic Programming
1007 Minimum Domino Rotations For Equal Row Medium Array, Greedy
1014 Best Sightseeing Pair Medium Array, Dynamic Programming
1028 Recover a Tree From Preorder Traversal Hard String, Tree, Depth First Search, Binary Tree
1072 Flip Columns For Maximum Number of Equal Rows Medium Array, Hash Table, Matrix
1079 Letter Tile Possibilities Medium Hash Table, String, Backtracking, Counting
1092 Shortest Common Supersequence Hard String, Dynamic Programming
1106 Parsing A Boolean Expression Hard String, Stack, Recursion
1123 Lowest Common Ancestor of Deepest Leaves Medium Hash Table, Tree, Depth First Search, Breadth First Search, Binary Tree
1128 Number of Equivalent Domino Pairs Easy Array, Hash Table, Counting
1233 Remove Sub-Folders from the Filesystem Medium Array, String, Depth-First Search, Trie
1261 Find Elements in a Contaminated Binary Tree Medium Hash Table, Tree, Depth First Search, Breadth First Search, Design, Binary Tree
1267 Count Servers that Communicate Medium Array, Depth First Search, Breadth First Search, Union Find, Matrix, Counting
1277 Count Square Submatrices with All Ones Medium Array, Dynamic Programming, Matrix
1295 Find Numbers with Even Number of Digits Easy Array, Math
1331 Rank Transform of an Array Easy Array, Hash Table, Sorting
1346 Check If N and Its Double Exist Easy Array, Hash Table, Two Pointers, Binary Search, Sorting
1352 Product of the Last K Numbers Medium Array, Math, Design, Data Stream, Prefix Sum
1358 Number of Substrings Containing All Three Characters Medium Hash Table, String, Sliding Window
1368 Minimum Cost to Make at Least One Valid Path in a Grid Hard Array, Breadth First Search, Graph, Heap (priority Queue), Matrix, Shortest Path
1399 Count Largest Group Easy Hash Table, Math
1400 Construct K Palindrome Strings Medium Hash Table, String, Greedy, Counting
1405 Longest Happy String Medium String, Greedy, Heap (Priority Queue)
1408 String Matching in an Array Easy Array, String, String Matching
1415 The k-th Lexicographical String of All Happy Strings of Length n Medium String, Backtracking
1422 Maximum Score After Splitting a String Easy String, Prefix Sum
1455 Check If a Word Occurs As a Prefix of Any Word in a Sentence Easy Two Pointers, String, String Matching
1462 Course Schedule IV Medium Depth First Search, Breadth First Search, Graph, Topological Sort
1475 Final Prices With a Special Discount in a Shop Easy Array, Stack, Monotonic Stack
1497 Check If Array Pairs Are Divisible by k Medium Array, Hash Table, Counting
1524 Number of Sub-arrays With Odd Sum Medium Array, Math, Dynamic Programming, Prefix Sum
1534 Count Good Triplets Easy Array, Enumeration
1545 Find Kth Bit in Nth Binary String Medium String, Recursion, Simulation
1574 Shortest Subarray to be Removed to Make Array Sorted Medium Array, Two Pointers, Binary Search, Stack, Monotonic Stack
1590 Make Sum Divisible by P Medium Array, Hash Table, Prefix Sum
1593 Split a String Into the Max Number of Unique Substrings Medium Hash Table, String, Backtracking
1639 Number of Ways to Form a Target String Given a Dictionary Hard Array, String, Dynamic Programming
1652 Defuse the Bomb Easy Array, Sliding Window
1671 Minimum Number of Removals to Make Mountain Array Hard Array, Binary Search, Dynamic Programming, Greedy
1718 Construct the Lexicographically Largest Valid Sequence Medium Array, Backtracking
1726 Tuple with Same Product Medium Array, Hash Table, Counting
1749 Maximum Absolute Sum of Any Subarray Medium Array, Dynamic Programming
1752 Check if Array Is Sorted and Rotated Easy Array
1760 Minimum Limit of Balls in a Bag Medium Array, Binary Search
1765 Map of Highest Peak Medium Array, Breadth First Search, Matrix
1769 Minimum Number of Operations to Move All Balls to Each Box Medium Array, String, Prefix Sum
1780 Check if Number is a Sum of Powers of Three Medium Math
1790 Check if One String Swap Can Make Strings Equal Easy Hash Table, String, Counting
1792 Maximum Average Pass Ratio Medium Array, Greedy, Heap (Priority Queue)
1800 Maximum Ascending Subarray Sum Easy Array
1813 Sentence Similarity III Medium Array, Two Pointers, String
1829 Maximum XOR for Each Query Medium Array, Bit Manipulation, Prefix Sum
1861 Rotating the Box Medium Array, Two Pointers, Matrix
1863 Sum of All Subset XOR Totals Easy Array, Math, Backtracking, Bit Manipulation, Combinatorics, Enumeration
1910 Remove All Occurrences of a Substring Medium String, Stack, Simulation
1920 Build Array from Permutation Easy Array, Simulation
1922 Count Good Numbers Medium Math, Recursion
1930 Unique Length-3 Palindromic Subsequences Medium Hash Table, String, Bit Manipulation, Prefix Sum
1942 The Number of the Smallest Unoccupied Chair Medium Array, Hash Table, Heap (Priority Queue)
1957 Delete Characters to Make Fancy String Easy String
1963 Minimum Number of Swaps to Make the String Balanced Medium Two Pointers, String, Stack, Greedy
1975 Maximum Matrix Sum Medium Array, Greedy, Matrix
1976 Number of Ways to Arrive at Destination Medium Dynamic Programming, Graph, Topological Sort, Shortest Path
1980 Find Unique Binary String Medium Array, Hash Table, String, Backtracking
2017 Grid Game Medium Array, Matrix, Prefix Sum
2033 Minimum Operations to Make a Uni-Value Grid Medium Array, Math, Sorting, Matrix
2044 Count Number of Maximum Bitwise-OR Subsets Medium Array, Backtracking, Bit Manipulation, Enumeration
2054 Two Best Non-Overlapping Events Medium Array, Binary Search, Dynamic Programming, Sorting, Heap (priority Queue)
2064 Minimized Maximum of Products Distributed to Any Store Medium Array, Binary Search
2070 Most Beautiful Item for Each Query Medium Array, Binary Search, Sorting
2071 Maximum Number of Tasks You Can Assign Hard Array, Binary Search, Greedy, Queue, Sorting, Monotonic Queue
2097 Valid Arrangement of Pairs Hard Depth First Search, Graph, Eulerian Circuit
2109 Adding Spaces to a String Medium Array, Two Pointers, String, Simulation
2115 Find All Possible Recipes from Given Supplies Medium Array, Hash Table, String, Graph, Topological Sort
2116 Check if a Parentheses String Can Be Valid Medium String, Stack, Greedy
2127 Maximum Employees to Be Invited to a Meeting Hard Depth First Search, Graph, Topological Sort
2140 Solving Questions With Brainpower Medium Array, Dynamic Programming
2145 Count the Hidden Sequences Medium Array, Prefix Sum
2161 Partition Array According to Given Pivot Medium Array, Two Pointers, Simulation
2176 Count Equal and Divisible Pairs in an Array Easy Array
2179 Count Good Triplets in an Array Hard Array, Binary Search, Divide And Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
2182 Construct String With Repeat Limit Medium Hash Table, String, Greedy, Heap (priority Queue), Counting
2185 Counting Words With a Given Prefix Easy Array, String, String Matching
2206 Divide Array Into Equal Pairs Easy Array, Hash Table, Bit Manipulation, Counting
2222 Number of Ways to Select Buildings Medium String, Dynamic Programming, Prefix Sum
2226 Maximum Candies Allocated to K Children Medium Array, Binary Search
2257 Count Unguarded Cells in the Grid Medium Array, Matrix, Simulation
2270 Number of Ways to Split Array Medium Array, Prefix Sum
2275 Largest Combination With Bitwise AND Greater Than Zero Medium Array, Hash Table, Bit Manipulation, Counting
2290 Minimum Obstacle Removal to Reach Corner Hard Array, Breadth-First Search, Graph, Heap (Priority Queue), Matrix, Shortest Path
2302 Count Subarrays With Score Less Than K Hard Array, Binary Search, Sliding Window, Prefix Sum
2335 Moving Pieces to Obtain a String Medium String, Two Pointers
2338 Count the Number of Ideal Arrays Hard Math, Dynamic Programming, Combinatorics, Number Theory
2342 Max Sum of a Pair With Equal Sum of Digits Medium Array, Hash Table, Sorting, Heap (priority Queue)
2349 Design a Number Container System Medium Hash Table, Design, Heap (priority Queue), Ordered Set
2364 Count Number of Bad Pairs Medium Array, Hash Table, Math, Counting
2375 Construct Smallest Number From DI String Medium String, Backtracking, Stack, Greedy
2379 Minimum Recolors to Get K Consecutive Black Blocks Easy String, Sliding Window
2381 Shifting Letters II Medium Array, String, Prefix Sum
2401 Longest Nice Subarray Medium Array, Bit Manipulation, Sliding Window
2415 Reverse Odd Levels of Binary Tree Medium Tree, Depth First Search, Breadth First Search, Binary Tree
2416 Sum of Prefix Scores of Strings Hard Array, String, Trie, Counting
2423 Remove Letter To Equalize Frequency Easy Hash Table, String, Counting
2425 Bitwise XOR of All Pairings Medium Array, Bit Manipulation, Brainteaser
2429 Minimize XOR Medium Greedy, Bit Manipulation
2444 Count Subarrays With Fixed Bounds Hard Array, Queue, Sliding Window, Monotonic Queue
2458 Height of Binary Tree After Subtree Removal Queries Hard Array, Tree, Depth-First Search, Breadth-First Search, Binary Tree
2460 Apply Operations to an Array Easy Array, Two Pointers, Simulation
2461 Maximum Sum of Distinct Subarrays With Length K Medium Array, Hash Table, Sliding Window
2463 Minimum Total Distance Traveled Hard Array, Dynamic Programming, Sorting
2466 Count Ways To Build Good Strings Medium Dynamic Programming
2467 Most Profitable Path in a Tree Medium Array, Tree, Depth First Search, Breadth First Search, Graph
2471 Minimum Number of Operations to Sort a Binary Tree by Level Medium Tree, Breadth First Search, Binary Tree
2490 Circular Sentence Easy String
2493 Divide Nodes Into the Maximum Number of Groups Hard Breadth First Search, Union Find, Graph
2501 Longest Square Streak in an Array Medium Array, Hash Table, Binary Search, Dynamic Programming, Sorting
2503 Maximum Number of Points From Grid Queries Hard Array, Two Pointers, Breadth First Search, Union Find, Sorting, Heap (priority Queue), Matrix
2516 Take K of Each Character From Left and Right Medium Hash Table, String, Sliding Window
2523 Closest Prime Numbers in Range Medium Math, Number Theory
2529 Maximum Count of Positive Integer and Negative Integer Easy Array, Binary Search, Counting
2537 Count the Number of Good Subarrays Medium Array, Hash Table, Sliding Window
2551 Put Marbles in Bags Hard Array, Greedy, Sorting, Heap (priority Queue)
2554 Maximum Number of Integers to Choose From a Range I Medium Array, Hash Table, Binary Search, Greedy, Sorting
2558 Take Gifts From the Richest Pile Easy Array, Heap (priority Queue), Simulation
2559 Count Vowel Strings in Ranges Medium Array, String, Prefix Sum
2560 House Robber IV Medium Array, Binary Search
2563 Count the Number of Fair Pairs Medium Array, Two Pointers, Binary Search, Sorting
2570 Merge Two 2D Arrays by Summing Values Easy Array, Hash Table, Two Pointers
2577 Minimum Time to Visit a Cell In a Grid Hard Array, Breadth First Search, Graph, Heap (priority Queue), Matrix, Shortest Path
2579 Count Total Number of Colored Cells Medium Math
2583 Kth Largest Sum in a Binary Tree Medium Tree, Breadth-First Search, Sorting, Binary Tree
2593 Find Score of an Array After Marking All Elements Medium Array, Hash Table, Sorting, Heap (priority Queue), Simulation
2594 Minimum Time to Repair Cars Medium Array, Binary Search
2601 Prime Subtraction Operation Medium Array, Math, Binary Search, Greedy, Number Theory
2641 Cousins in Binary Tree II Medium Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
2657 Find the Prefix Common Array of Two Arrays Medium Array, Hash Table, Bit Manipulation
2658 Maximum Number of Fish in a Grid Medium Array, Depth First Search, Breadth First Search, Union Find, Matrix
2661 First Completely Painted Row or Column Medium Array, Hash Table, Matrix
2683 Neighboring Bitwise XOR Medium Array, Bit Manipulation
2684 Maximum Number of Moves in a Grid Medium Array, Dynamic Programming, Matrix
2685 Count the Number of Complete Components Medium Depth First Search, Breadth First Search, Union Find, Graph
2696 Minimum String Length After Removing Substrings Easy String, Stack, Simulation
2698 Find the Punishment Number of an Integer Medium Math, Backtracking
2762 Continuous Subarrays Medium Array, Queue, Sliding Window, Heap (priority Queue), Ordered Set, Monotonic Queue
2779 Maximum Beauty of an Array After Applying Operation Medium Array, Binary Search, Sliding Window, Sorting
2780 Minimum Index of a Valid Split Medium Array, Hash Table, Sorting
2799 Count Complete Subarrays in an Array Medium Array, Hash Table, Sliding Window
2818 Apply Operations to Maximize Score Hard Array, Math, Stack, Greedy, Sorting, Monotonic Stack, Number Theory
2825 Make String a Subsequence Using Cyclic Increments Medium Two Pointers, String
2843 Count Symmetric Integers Easy Math, Enumeration
2845 Count of Interesting Subarrays Medium Array, Hash Table, Prefix Sum
2872 Maximum Number of K-Divisible Components Hard Tree, Depth First Search
2873 Maximum Value of an Ordered Triplet I Easy Array
2874 Maximum Value of an Ordered Triplet II Medium Array
2914 Minimum Changes to Make Binary String Beautiful Medium String
2924 Find Champion II Medium Graph
2938 Separate Black and White Balls Medium Two Pointers, String, Greedy
2940 Find Building Where Alice and Bob Can Meet Hard Array, Binary Search, Stack, Monotonic Stack
2948 Make Lexicographically Smallest Array by Swapping Elements Medium Array, Union Find, Sorting
2962 Count Subarrays Where Max Element Appears at Least K Times Medium Array, Sliding Window
2965 Find Missing and Repeated Values Easy Array, Hash Table, Math, Matrix
2981 Find Longest Special Substring That Occurs Thrice I Medium Hash Table, String, Binary Search, Sliding Window, Counting
2999 Count the Number of Powerful Integers Hard Math, String, Dynamic Programming
3011 Find if Array Can Be Sorted Medium Array, Bit Manipulation, Sorting
3017 Count the Number of Houses at a Certain Distance II Hard Graph, Prefix Sum
3042 Count Prefix and Suffix Pairs I Easy Array, String, Trie, Rolling Hash, String Matching, Hash Function
3043 Find the Length of the Longest Common Prefix Medium Array, Hash Table, String, Trie
3066 Minimum Operations to Exceed Threshold Value II Medium Array, Heap (priority Queue), Simulation
3090 Shortest Subarray With OR at Least K II Medium Array, Bit Manipulation, Sliding Window
3105 Longest Strictly Increasing or Strictly Decreasing Subarray Easy Array
3108 Minimum Cost Walk in Weighted Graph Hard Array, Bit Manipulation, Union Find, Graph
3133 Minimum Array End Medium Bit Manipulation
3151 Special Array I Easy Array
3152 Special Array II Medium Array, Binary Search, Prefix Sum
3160 Find the Number of Distinct Colors Among the Balls Medium Array, Hash Table, Simulation
3163 String Compression III Medium String
3169 Count Days Without Meetings Medium Array, Sorting
3174 Clear Digits Easy String, Stack, Simulation
3191 Minimum Operations to Make Binary Array Elements Equal to One I Medium Array, Bit Manipulation, Queue, Sliding Window, Prefix Sum
3203 Find Minimum Diameter After Merging Two Trees Hard Tree, Depth First Search, Breadth First Search, Graph
3208 Alternating Groups II Medium Array, Sliding Window
3223 Minimum Length of String After Operations Medium Hash Table, String, Counting
3243 Shortest Distance After Road Addition Queries I Medium Array, Breadth First Search, Graph
3254 Find the Power of K-Size Subarrays I Medium Array, Sliding Window
3264 Final Array State After K Multiplication Operations I Easy Array, Math, Heap (priority Queue), Simulation
3272 Find the Count of Good Integers Hard Hash Table, Math, Combinatorics, Enumeration
3306 Count of Substrings Containing Every Vowel and K Consonants II Medium Hash Table, String, Sliding Window
3341 Find Minimum Time to Reach Last Room I Medium Array, Graph, Heap (priority Queue), Matrix, Shortest Path
3342 Find Minimum Time to Reach Last Room II Medium Array, Graph, Heap (priority Queue), Matrix, Shortest Path
3356 Zero Array Transformation II Medium Array, Binary Search, Prefix Sum
3375 Minimum Operations to Make Array Values Equal to K Easy Array, Hash Table
3392 Count Subarrays of Length Three With a Condition Easy Array
3394 Check if Grid can be Cut into Sections Medium Array, Sorting
3396 Minimum Number of Operations to Make Elements in Array Distinct Easy Array, Hash Table

🚀 Getting Started

  1. Clone the repository:
git clone https://github.com/tomPlanche/leetcode.git
  1. Navigate to a solution:
cd problems/id_<problem_number>
  1. Run the solution:
cargo run

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👤 Author

Tom Planche