Skip to content

thegovyadina/leetcode-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LeetCode solutions in Rust

Rust checks and tests Commit Activity

Easy Medium Hard Total

Disclaimer

This repository contains my personal solutions to LeetCode problems, created for educational purposes and personal reference only. All problem descriptions, names, and related content are the property of LeetCode (https://leetcode.com/).

  • The solutions provided here are meant for learning and study purposes only
  • Problem statements are intentionally omitted or minimized to avoid copyright issues
  • Links to original problems are provided for proper reference
  • This is not an official LeetCode repository and is not affiliated with LeetCode

If you're a LeetCode representative and have concerns about this repository, please contact me and I'll address them promptly.

License

This repository is licensed under the MIT Licenseβ€”see the LICENSE file for details.

Note that this license applies only to my solution code and not to the LeetCode problem statements themselves.

Problems

Problem Solution Approach
🟒 1. Two Sum πŸ¦€ Hash Map
🟠 3. Longest Substring Without Repeating Characters πŸ¦€ Hash Map, Two Pointers
🟒 13. Roman to Integer πŸ¦€
🟒 20. Valid Parentheses πŸ¦€ Stack
🟒 21. Merge Two Sorted Lists πŸ¦€
🟠 33. Search in Rotated Sorted Array πŸ¦€ Binary Search
🟠 39. Combination Sum πŸ¦€ Backtracking
🟠 96. Unique Binary Search Trees πŸ¦€ Dynamic Programming
πŸ”΄ 126. Word Ladder II πŸ¦€ Breadth-First Search
🟠 215. Kth Largest Element in an Array πŸ¦€
🟠 227. Basic Calculator II πŸ¦€ Stack
🟒 263. Ugly Number πŸ¦€
🟒 504. Base 7 πŸ¦€
πŸ”΄ 552. Student Attendance Record II πŸ¦€ Dynamic Programming
🟒 599. Minimum Index Sum of Two Lists πŸ¦€ Hash Map
🟒 645. Set Mismatch πŸ¦€
🟠 707. Design Linked List πŸ¦€
🟠 739. Daily Temperatures πŸ¦€ Monotonic Stack
🟠 797. All Paths From Source to Target πŸ¦€ Depth-First Search
🟒 905. Sort Array By Parity πŸ¦€
🟠 954. Array of Doubled Pairs πŸ¦€
🟠 957. Prison Cells After N Days πŸ¦€
🟒 1137. N-th Tribonacci Number πŸ¦€
🟒 1431. Kids With the Greatest Number of Candies πŸ¦€
🟒 1464. Maximum Product of Two Elements in an Array πŸ¦€
🟠 1599. Maximum Profit of Operating a Centennial Wheel πŸ¦€
🟒 1784. Check if Binary String Has at Most One Segment of Ones πŸ¦€
🟠 1814. Count Nice Pairs in an Array πŸ¦€ HashMap
πŸ”΄ 1866. Number of Ways to Rearrange Sticks With K Sticks Visible πŸ¦€ Dynamic Programming
🟒 1876. Substrings of Size Three with Distinct Characters πŸ¦€
🟒 1910. Remove All Occurrences of a Substring πŸ¦€ Stack
🟒 1974. Minimum Time to Type Word Using Special Typewriter πŸ¦€
🟒 2099. Find Subsequence of Length K With the Largest Sum πŸ¦€
🟠 2201. Count Artifacts That Can Be Extracted πŸ¦€
🟠 2270. Number of Ways to Split Array πŸ¦€
πŸ”΄ 2338. Count the Number of Ideal Arrays πŸ¦€ Dynamic Programming, Combinatorics
🟒 2399. Check Distances Between Same Letters πŸ¦€
🟠 2442. Count Number of Distinct Integers After Reverse Operations πŸ¦€ Hash Set
🟠 2471. Minimum Number of Operations to Sort a Binary Tree by Level πŸ¦€
🟠 2857. Count Pairs of Points With Distance k πŸ¦€ Hash Map
πŸ”΄ 2902. Count of Sub-Multisets With Bounded Sum πŸ¦€ Dynamic Programming
🟠 2933. High-Access Employees πŸ¦€ Hash Map
πŸ”΄ 2940. Find Building Where Alice and Bob Can Meet πŸ¦€ Priority Queue
🟠 3021. Alice and Bob Playing Flower Game πŸ¦€
🟒 3174. Clear digits πŸ¦€ Stack
🟠 3208. Alternating Groups II πŸ¦€ Sliding Window
🟠 3212. Count Submatrices With Equal Frequency of X and Y πŸ¦€ Dynamic Programming
🟒 3270. Find the Key of the Numbers πŸ¦€
🟒 3289. The Two Sneaky Numbers of Digitville πŸ¦€

Development Setup

After cloning the repository, run this command to set up Git hooks:

git config core.hooksPath .githooks

If you want to run memory benchmarks, you need to install valgrind and massif-visualizer on your system. On Ubuntu, you can do this with:

sudo apt install valgrind massif-visualizer

How to add a new problem

  1. Create a new file in src/problems/ directory with the name of the problem in snake_case with format pNNNN_problem_title.rs, where NNNN is the number of the problem on LeetCode in 4-digit format with leading zeros and problem_title is the name of the problem in snake_case.

  2. Use the following template for the file:

//! # LeetCode Problem: [Problem Number] - [Problem Title]
//!
//! Difficulty: [Easy/Medium/Hard]
//!
//! Link: https://leetcode.com/problems/[problem-slug]/
//!
//! ## Complexity Analysis
//! - Time Complexity: O(n) - Describe the time complexity of your solution.
//! - Space Complexity: O(n) - Describe the space complexity of your solution.

pub struct Solution;

// Copy the problem title from LeetCode
impl Solution {
    pub fn problem_title() {
        // Your solution here
    }
}

// Tests are written in the same file and are mandatory
#[cfg(test)]
mod tests {
    use super::Solution;

    #[test]
    fn test_problem_title() {
        // Your tests here
    }
}

Complexity analysis is optional but recommended. If you're not sure about the complexity of your solution, you can leave it out.

  1. Update the lib.rs file to include the new problem file. Add the following line at the end of the file:
pub mod pNNNN_problem_title;
  1. Add the problem to the Problems table in README.md
  2. Run the tests with cargo test to make sure everything is working.
  3. Commit your changes.

Benchmarking

For benchmarking, the criterion crate is used.

To add a new benchmark, create a new file in the ./benches/ directory and add a new [[branch]] section in Cargo.toml.

To execute a benchmark, run one of the following commands:

# Run all benchmarks
cargo bench

# Run all benchmarks defined in a specific file
cargo bench --bench p1814

Also, you can run them more precisely using special syntax. E.g.:

# Run only the benchmark for the specific number 1234
cargo bench --bench p1814 "loop_based_1234"

# Run all loop-based benchmarks
cargo bench --bench p1814 "loop_based_.*"

See the Criterion documentation for more information or run cargo help bench.

Memory profiling

For memory profiling, create a new file in the /benches/ directory and use IMPL environment variable to specify implementations to profile. See ./benches/p1910_memory.rs for an example. Add a new [[bench]] section in Cargo.toml to define the benchmark.

To run a memory benchmark, execute the following command:

. ./profile.sh <benchmark_name> <implementation_ids>

The script accepts the following parameters:

  • <benchmark_name>: Name of the benchmark file without the .rs extension (e.g., p1910_memory)
  • <implementation_ids>: Space-separated list of implementation IDs to profile (e.g., 1 2 3)

Example usage

To compare memory usage between implementation 1 and 2 of the p1910_memory benchmark:

. ./profile.sh p1910_memory 1 2

This will:

  1. Run each implementation through Valgrind's Massif tool
  2. Generate memory profile data in the ./tmp/ directory
  3. Create output files like and p1910_memory_massif_1.out``p1910_memory_profile_1.txt
  4. Visualize the profiles using massif-visualizer: massif-visualizer tmp/p1910_memory_massif_1.out

About

LeetCode solutions written in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •