ActiveVariable / leetcode

leetcode.com solutions with unit testing (VSCode+CMake+Boost.Test)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

leetcode

Personal notes for leetcode.com solutions with support for local building and unit testing.

Stats

Top interviewed: 76 / 145

In total: 109 / 1411

Solutions that are 99.00%+ in running time: 66

Solutions

  1. Two Sum
  • Brutal force: 72ms (top 43.79%)
  • Hash table (map): 4ms (top 100.00%)
  1. Add Two Numbers
  • Linked list: 16ms (top 100.00%)
  1. Longest Substring Without Repeating Characters
  • Hash table (map): 8ms (top 100.00%)
  1. Median of Two Sorted Arrays
  • Non-recursive: 28 ms (top 96.13%)
  • Recursion: 16 ms (top 100.00%)
  1. Longest Palindromic Substring
  • Brutal force: 8ms (top 87.41%)
  • Manacher's algorithm: 0ms (top 100.00%)
  1. ZigZag Conversion
  • Brutal force: 40ms (top 55.26%)
  • Fast for-loop: 8ms (top 100.00%)
  1. Reverse Integer
  • Use INT_MAX: 4ms (top 100.00%)
  1. String to Integer (atoi)
  • Use INT_MAX: 8ms (top 99.90%)
  1. Palindrome Number
  • Lookup table: 52ms (top 100.00%)
  1. Regular Expression Matching
  • Dynamic programming: 4ms (top 100.00%)
  1. Container With Most Water
  • Two pointers: 12ms (top 100.00%)
  1. Integer to Roman
  • Two arrays: 28ms (top 99.20%)
  • Lookup table: 28ms (top 99.20%)
  1. Roman to Integer
  • Two vectors: 36ms (top 98.41%)
  • Map: 44ms (top 97.72%)
  1. Longest Common Prefix
  • 4ms (top 100.00%)
  1. 3Sum
  • Two pointers: 112ms (top 99.88%)
  1. 3Sum Closest
  • Two pointers: 8ms (top 100.00%)
  1. Letter Combinations of a Phone Number
  • Deque: 4ms (top 100.00%)
  1. 4Sum
  • Set: 40ms (top 82.57%)
  • Fast for-loop: 20ms (top 90.04%)
  1. Remove Nth Node From End of List
  • One pass: 8ms (top 100.00%)
  1. Valid Parentheses
  • Stack: 4ms (top 100.00%)
  1. Merge Two Sorted Lists
  • Two swaps: 12ms (top 100.00%)
  1. Generate Parentheses
  • Deque: 8ms (top 100.00%)
  1. Merge k Sorted Lists
  • Use 021: 176ms (top 25.72%)
  • Priority queue: 32ms (top 98.10%)
  1. Swap Nodes in Pairs
  • 0ms (top 100.00%)
  1. Reverse Nodes in k-Group
  1. Remove Duplicates from Sorted Array
  1. Remove Element
  • 0ms (top 100.00%)
  1. Implement strStr()
  • 8ms (top 98.99%)
  • KMP: 4ms (top 91.65%)
  1. Divide Two Integers
  • All int without <limits.h>: 8ms (top 100.00%)
  1. Substring with Concatenation of All Words
  1. Next Permutation
  • 4ms (top 96.34%)
  1. Longest Valid Parentheses
  • 4ms (top 96.65%)
  1. Search in Rotated Sorted Array
  • 0ms (top 100.00%)
  1. Find First and Last Position of Element in Sorted Array
  • 8ms (top 98.52%)
  1. Search Insert Position
  • 4ms (top 98.31%)
  1. Valid Sudoku
  • 8ms (top 99.96%)
  1. Sudoku Solver
  • 4ms (top 95.52%)
  1. Count and Say
  • 4ms (top 97.84%)
  1. Combination Sum
  • 8ms (top 95.15%)
  1. Combination Sum II
  • 4ms (top 98.92%)
  1. First Missing Positive
  • 4ms (top 96.34%)
  1. Trapping Rain Water
  • 4ms (top 99.71%)
  1. Multiply Strings
  • C++ interface: 8ms (top 72.26%)
  • C interface: 4ms (top 97.53%)
  1. Wildcard Matching
  • 4ms (top 99.84%)
  1. Jump Game II
  • 8ms (top 95.32%)
  1. Permutations
  • Recursive: 20ms (top 22.83%)
  • Better loop: 12ms (top 96.86%)
  • Backtracking: 4ms (top 96.41%)
  1. Permutations II
  • 8ms (top 96.44%)
  1. Rotate Image
  • Direct rotation: 4ms (top 98.21%)
  • Flip & swap: 4ms (top 98.21%)
  1. Group Anagrams
  1. Pow(x, n)
  • 4ms (top 96.07%)
  1. N-Queens
  • 4ms (top 99.62%)
  1. Maximum Subarray
  • Divide and conquer: 8ms (top 96.32%)
  • Kadane: 4ms (top 99.88%)
  • DP: 0ms (top 100.00%)
  1. Spiral Matrix
  • 0ms (top 100.00%)
  1. Jump Game
  • 4ms (top 99.99%)
  1. Merge Intervals
  • 8ms (top 100.00%)
  1. Permutation Sequence
  • 0ms (top 100.00%)
  1. Unique Paths
  • 4ms (top 93.30%)
  1. Unique Paths II
  • 0ms (top 100.00%)
  1. Minimum Path Sum
  • 4ms (top 99.39%)
  1. Plus One
  • 0ms (top 100.00%)
  1. Sqrt(x)
  • 0ms (top 100.00%)
  1. Climbing Stairs
  • 4ms (top 90.67%)
  1. Set Matrix Zeroes
  1. Search a 2D Matrix
  • 4ms (top 99.56%)
  1. Sort Colors
  • 0ms (top 100.00%)
  1. Minimum Window Substring
  • sliding window: 32ms (top 38.25%)
  • optimized sliding window: 44ms (top 19.33%)
  • hash: 4ms (top 99.92%)
  1. Subsets
  • 8ms (top 90.16%)
  • 4ms (top 98.41%)
  1. Word Search
  1. Largest Rectangle in Histogram
  • recursion: 32ms (top 14.84%)
  • non-recursion: 4ms (top 100.00%)
  1. Merge Sorted Array
  • additional space: 4ms (top 96.53%)
  • 3 pointers: 0ms (top 100.00%)
  1. Decode Ways
  • 4ms (top 90.46%)
  1. Binary Tree Inorder Traversal
  • Recursion: 4ms (top 87.58%)
  • Stack: 0ms (top 100.00%)
  1. Validate Binary Search Tree
  • 4ms (top 99.99%)
  1. Symmetric Tree
  • 0ms (top 100.00%)
  1. Binary Tree Level Order Traversal
  • 12ms (top 25.64%)
  • one queue: 8ms (top 82.74%)
  1. Binary Tree Zigzag Level Order Traversal
  • 4ms (top 89.75%)
  1. Maximum Depth of Binary Tree
  1. Construct Binary Tree from Preorder and Inorder Traversal
  • 40ms (top 19.30%)
  • hash: 16ms (top 89.76%)
  1. Convert Sorted Array to Binary Search Tree
  1. Minimum Depth of Binary Tree
  • 4ms (top 99.83%)
  1. Populating Next Right Pointers in Each Node
  1. Pascal's Triangle
  • swap: 4ms (top 51.26%)
  • resize: 0ms (top 100.00%)
  1. Triangle
  • 0ms (top 100.00%)
  1. Best Time to Buy and Sell Stock
  • priority queue: 12ms (top 17.37%)
  • 4ms (top 97.69%)
  1. Best Time to Buy and Sell Stock II
  • 0ms (top 100.00%)
  1. Best Time to Buy and Sell Stock III
  • 0ms (top 100.00%)
  1. Binary Tree Maximum Path Sum
  1. Valid Palindrome
  • 4ms (top 99.16%)
  1. Word Ladder
  1. Longest Consecutive Sequence
  • hash: 12ms (top 68.65%)
  • vector::unique 8ms (top 94.78%)
  1. Surrounded Regions
  1. Palindrome Partitioning
  • backtrack: 136ms (top 8.58%)
  • shared DP: 20ms (top 70.68%)
  1. Gas Station
  • 0ms (top 100.00%)
  1. Single Number
  • 4ms (top 99.88%)
  1. Evaluate Reverse Polish Notation
  1. Find Minimum in Rotated Sorted Array
  • 0ms (top 100.00%)
  1. Repeated DNA Sequences
  • 3-bit hash: 52ms (top 81.78%)
  • 2-bit hash: 4ms (top 99.71%)
  1. House Robber
  • 0ms (top 100.00%)
  1. House Robber II
  • 0ms (top 100.00%)
  1. Kth Largest Element in an Array
  • 4ms (top 99.81%)
  1. Maximal Square
  • 8ms (top 99.64%)
  1. Search a 2D Matrix II
  1. First Bad Version
  • 0ms (top 100.00%)
  1. Perfect Squares
  • 0ms (top 100.00%)
  • Tree search: 92ms (top 79.03%)
  1. Find the Duplicate Number
  • Binary search: 8ms (top 94.55%)
  • Two pointers: 4ms (top 99.76%)
  1. Longest Increasing Subsequence
  • 0ms (top 100.00%)
  1. Remove Invalid Parentheses
  1. Intersection of Two Arrays
  • Set: 4ms (top 99.21%)
  • Map: 4ms (top 99.21%)
  1. Intersection of Two Arrays II
  • 4ms (top 99.54%)

Problem categories

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
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, 301
Backtracking 017, 022, 037, 039, 040, 046, 047, 051, 060, 078
DP 053, 062, 063, 064, 070, 091, 120, 131, 198, 213, 221, 300

Note that binary tree problems are mostly included in the Recursion category.


Environment

《Ubuntu计算机视觉开发环境配置(Python/C++)》 《Manjaro计算机视觉开发环境配置(Python/C++)》 《Windows配置VSCode+CMake+Ninja+Boost.Test的C++开发环境》

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.json file 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 install

Usage

Each 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:

  1. Change the line set(PROBLEM_NAME {Problem_Folder}) in CMakeLists.txt to 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").

  2. Press Ctrl + Shift + P to bring up the Command Palette of VSCode. Type in CMake, and look for a CMake: Configure command, select it. It will configure the cache files and makefile which are located in build folder by default.

  3. Type in or look for a CMake: Build command in the Command Palette and execute it. It will compile the source codes of the solution that you previously chose.

  4. Debug Press Ctrl + Shift + D or click the bug icon on the left. The .vscode/launch.json for debugging is already set up for you, and both GDB and LLDB is supported.

    gdb: Choose (gdb) Launch and you're good to go.

    lldb: Choose (lldb) Launch. You may need to disable and re-enable ms-vscode.cpptools and mitaki28.vscode-clang extensions in case the variables won't properly display in "Watch" Panel. Data visualization is supported (https://github.com/vadimcn/vscode-lldb/wiki/Data-visualization).

  5. Unit testing Type in or look for a CMake: Run tests command in the Command Palette and execute it. It will run all test cases in solution_test.cpp. Feel free to add your own test cases.

Clear build folder:

  1. Press Ctrl + Shift + P to bring up the Command Palette. Type in Task: Run Task and press enter. Choose clean will delete everything in the build folder. Bear in mind that you need to configure the project after cleaning the folder or switching to another solution, by either executing Task: Run Task -> configure or CMake: Configure.

About

leetcode.com solutions with unit testing (VSCode+CMake+Boost.Test)

License:Apache License 2.0


Languages

Language:C++ 99.2%Language:CMake 0.5%Language:Python 0.3%