Personal notes for leetcode.com solutions with support for local building and unit testing.
Top interviewed: 76 / 145 52%
In total: 114 / 2143 5%
99.00%+ in running time: 79 / 114 69%
- Hash table (unorderd_map): 4ms (top 99.54%)
- Linked list: 16ms (top 100.00%)
- Hash table (vector): 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- Lookup table: 52ms (top 100.00%)
- Dynamic programming: 4ms (top 100.00%)
- Two pointers: 12ms (top 100.00%)
- 4ms (top 100.00%)
- Two pointers: 112ms (top 99.88%)
- Two pointers: 8ms (top 100.00%)
- 0ms (top 100.00%)
- Stack: 4ms (top 100.00%)
- Two swaps: 12ms (top 100.00%)
- 0ms (top 100.00%)
- 20ms (top 95.79%)
- 20ms (top 99.75%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 40ms (top 88.85%)
- 4ms (top 96.34%)
- 4ms (top 96.65%)
- 0ms (top 100.00%)
- 8ms (top 98.52%)
- 4ms (top 98.31%)
- 8ms (top 99.96%)
- 4ms (top 95.52%)
- 4ms (top 97.84%)
- 8ms (top 95.15%)
- 4ms (top 98.92%)
- 4ms (top 96.34%)
- 4ms (top 99.71%)
- 4ms (top 99.84%)
- 8ms (top 95.32%)
- 8ms (top 96.44%)
- 40ms (top 95.50%)
- 4ms (top 96.07%)
- 4ms (top 99.62%)
- 0ms (top 100.00%)
- 28ms (top 99.98%)
- 8ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 99.39%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 44ms (top 98.55%)
- 4ms (top 99.56%)
- sliding window: 32ms (top 38.25%)
- optimized sliding window: 44ms (top 19.33%)
- hash: 4ms (top 99.92%)
- 0ms (top 100.00%)
- Recursion - list: 4ms (top 87.58%)
- Recursion - vector: 4ms (top 43.93%)
- Stack: 0ms (top 100.00%)
- Stack: 0ms (top 100.00%)
- 4ms (top 76.73%)
- 4ms (top 63.24%)
- 8ms (top 75.97%)
- 4ms (top 99.83%)
- 4ms (top 99.83%)
- 16ms (top 95.40%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 68ms (top 99.95%)
- 16ms (top 99.85%)
- 4ms (top 99.16%)
- 28ms (top 99.56%)
- Graph + BFS: 138ms (top 67.54%)
- Graph + BiDir BFS: 131ms (top 69.50%)
- BiDir BFS: 560ms (top 34.48%)
- 4ms (top 99.88%)
- 0ms (top 100.00%)
- 4ms (top 99.88%)
- 12ms (top 91.72%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 99.81%)
- 8ms (top 99.64%)
- 84ms (top 72.87%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 12ms (top 68.31%)
- 4ms (top 99.54%)
- 0ms (top 100.00%)
- 24ms (top 96.91%)
| Category | Problems |
|---|---|
| Sort/search | 026, 031, 036, 041, 048, 054, 055, 056, 073, 074, 075, 118, 121, 122, 123, 128, 134, 136, 153, 215, 240, 278 |
| String | 005, 006, 008, 010, 014, 028, 038, 043, 044, 127 |
| Numerical | 007, 009, 029, 050, 066, 069, 279, 564 |
| Hash/Lookup | 001, 003, 012, 013, 020, 030, 049, 076, 187, 349, 350 |
| Stack/Queue/Deque | 023, 032, 094, 102, 103, 116, 150 |
| Linked list | 002, 019, 021, 024 |
| Two pointers | 011, 015, 016, 018, 027, 042, 084, 088, 125, 287 |
| Recursion | 004, 025, 033, 034, 035, 045, 079, 098, 101, 104, 105, 108, 111, 124, 130, 144, 145, 301, 540 |
| Backtracking | 017, 022, 037, 039, 040, 046, 047, 051, 060, 078 |
| DP | 053, 062, 063, 064, 070, 091, 120, 131, 198, 213, 221, 300, 864 |
Note that binary tree problems are mostly included in the
Recursioncategory.
- Ubuntu 21.10 / Manjaro 19.0 / Windows 10 / macOS Catalina 10.15.3
- Visual Studio Code with following extensions:
- C/C++ (ms-vscode.cpptools)
- C/C++ Clang Command Adapter (mitaki28.vscode-clang) (optional)
- CMake (twxs.cmake)
- CMake Tools (vector-of-bool.cmake-tools)
- CodeLLDB (vadimcn.vscode-lldb) (optional)
sudo snap install --classic code sudo apt-get install pylint
- clang (used by default. You can use other tools as long as your
.vscode/c_cpp_properties.jsonfile is modified correctly)
wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt-get update
sudo apt-get install clang-8 lldb-8- CMake
- Boost (for unit testing)
wget -O boost_1_70_0.tar.gz https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
tar xzvf boost_1_70_0.tar.gz
cd boost_1_70_0/
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev
./bootstrap.sh --prefix=/usr/
./b2
sudo ./b2 installEach folder contains 3 files: solution.hpp, solution.cpp and solution_test.cpp. The first folder 001-Two-Sum contains an additional main.cpp file, in case you don't feel like using unit testing and you can test your own cases in main.cpp.
Add following lines to your settings.json to configure the building and linting:
{
"clang.executable": "clang++",
"clang.cflags": ["c11"],
"clang.cxxflags": ["-std=c++17"],
"lldb.executable": "lldb"
}Steps to build a solution:
-
Change the line
set(PROBLEM_NAME {Problem_Folder})inCMakeLists.txtto choose the problem you want to solve, in which{Problem_Folder}is the folder name of the problem. For example,set(PROBLEM_NAME "001-Two-Sum"). -
Press
Ctrl+Shift+Pto bring up the Command Palette of VSCode. Type inCMake, and look for aCMake: Configurecommand, select it. It will configure the cache files and makefile which are located inbuildfolder by default. -
Type in or look for a
CMake: Buildcommand in the Command Palette and execute it. It will compile the source codes of the solution that you previously chose. -
Debug Press
Ctrl+Shift+Dor click the bug icon on the left. The.vscode/launch.jsonfor debugging is already set up for you, and both GDB and LLDB is supported.gdb: Choose
(gdb) Launchand you're good to go.lldb: Choose
(lldb) Launch. You may need to disable and re-enablems-vscode.cpptoolsandmitaki28.vscode-clangextensions in case the variables won't properly display in "Watch" Panel. Data visualization is supported (https://github.com/vadimcn/vscode-lldb/wiki/Data-visualization). -
Unit testing Type in or look for a
CMake: Run testscommand in the Command Palette and execute it. It will run all test cases insolution_test.cpp. Feel free to add your own test cases.
Clear build folder:
- Press
Ctrl+Shift+Pto bring up the Command Palette. Type inTask: Run Taskand pressenter. Choosecleanwill delete everything in thebuildfolder. Bear in mind that you need to configure the project after cleaning the folder or switching to another solution, by either executingTask: Run Task->configureorCMake: Configure.