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.
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.
After cloning the repository, run this command to set up Git hooks:
git config core.hooksPath .githooksIf 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-
Create a new file in
src/problems/directory with the name of the problem in snake_case with formatpNNNN_problem_title.rs, whereNNNNis the number of the problem on LeetCode in 4-digit format with leading zeros andproblem_titleis the name of the problem in snake_case. -
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.
- Update the
lib.rsfile to include the new problem file. Add the following line at the end of the file:
pub mod pNNNN_problem_title;- Add the problem to the Problems table in
README.md - Run the tests with
cargo testto make sure everything is working. - Commit your changes.
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 p1814Also, 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.
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.rsextension (e.g.,p1910_memory)<implementation_ids>: Space-separated list of implementation IDs to profile (e.g.,1 2 3)
To compare memory usage between implementation 1 and 2 of the p1910_memory benchmark:
. ./profile.sh p1910_memory 1 2This will:
- Run each implementation through Valgrind's Massif tool
- Generate memory profile data in the
./tmp/directory - Create output files like and
p1910_memory_massif_1.out``p1910_memory_profile_1.txt - Visualize the profiles using massif-visualizer:
massif-visualizer tmp/p1910_memory_massif_1.out