Readme File¶
Official Big-O Cheat Sheet¶
Here is the Offical Big-O Sheat Poster:
Here is the Common Data Structure Operations:
How to calculate the time and space complexity?¶
The depth of recursion can affect the space, required by an algorithm. Each recursive function call usually needs to allocate some additional memory (for temproary data) to process its argument, so a recursive algorithm will require space O(depth of recursion).
The examples of linear sum algorithm and binary sum algorithm:
def linear_sum(S: List[int], stop: int) -> int:
""" Using linear recursion to calculate sum of all elements of the array """
# time:O(N) space:O(N)
if (stop == 1):
return S[0]
else:
return linear_sum(S, stop-1) + S[stop-1]
def binary_sum(S: List[int], start: int, stop: int) -> int:
""" Using binary recursion to calculate sum of all elements of array
Return the sum of the numbers in implicit slice S[start:stop]. """
# time:O(N) space:O(log n)
if start >= stop: # zero elements in slice
return 0
elif start == stop-1: # one element in slice
return S[start]
else: # two or more elements in slice
mid = (start + stop) // 2
return binary_sum(S, start, mid) + binary_sum(S, mid, stop)
The binary sum algorithm uses O(log n) improves over the linear sum algorithm uses O(n).
Solutions list¶
# |
Title |
Level |
Time |
Space Note |
Tags |
---|---|---|---|---|---|
1 |
Easy |
O(N) |
O(N) |
HashMap |
|
168 |
Medium |
O(log N) |
O(log N) |
basic |
|
10 |
Hard |
O(NM) |
O(NM) |
Dynamic Programming |
|
13 |
Easy |
O(N) |
O(log N) |
Basic |
|
1239 |
Medium |
O(N) |
O(N) |
DFS |
|
1192 |
Hard |
O(N) |
O(N + M) |
DFS |
|
565 |
Medium |
O(N) |
O(1) |
Basic |
|
162 |
Medium |
O(log N) |
O(1) |
Binary Search |
|
657 |
Easy |
O(N) |
O(1) |
Basic |
|
1048 |
Medium |
O(N^2) |
O(N) |
Stack |
|
3 |
Medium |
O(N) |
O(N) |
Two Pointers |
|
2260 |
Medium |
O(N) |
O(N) |
Two Pointers |
|
547 |
Medium |
O(N^2) |
O(N^2) |
DFS |
|
207 |
Medium |
O(N + M) |
O(N + M) |
Topological Sorting |
|
300 |
Medium |
O(N^2) |
O(N) |
Dynamic Programming |
|
64 |
Medium |
O(NM) |
O(NM) |
DFS |
|
34 |
Medium |
O(log N) |
O(1) |
Stack |
|
53 |
Easy |
O(N) |
O(1) |
Basic |
|
71 |
Medium |
O(N) |
O(N) |
Stack |
|
78 |
Medium |
O(N*2^N) |
O(N*2^N) |
Backtracking |
|
91 |
Medium |
O(N) |
O(N) |
Dynamic Programming |
|
1763 |
Easy |
O(N* log N) |
O(N) |
Divide and Conquer |
|
217 |
Easy |
O(N) |
O(N) |
HashMap |
|
283 |
Easy |
O(N) |
O(1) |
Fast and Slow Pointers |
|
36 |
Medium |
O(N^2) |
O(N) |
BFS |
|
1704 |
Easy |
O(N) |
O(1) |
Two Pointers |
|
122 |
Medium |
O(N) |
O(1) |
Basic |
|
121 |
Easy |
O(N) |
O(1) |
Dynamic Programming |
|
714 |
Medium |
O(N) |
O(1) |
Dynamic Programming |
|
944 |
Easy |
O(NM) |
O(1) |
Basic |
|
44 |
Hard |
O(NM) |
O(N) |
Dynamic Programming |
|
2280 |
Medium |
O(N log N) |
O(1) |
Basic |
|
496 |
Easy |
O(N + M) |
O(M) |
Stack |
|
503 |
Medium |
O(N) |
O(N) |
Stack |
|
739 |
Medium |
O(N) |
O(N) |
Stack |
|
2281 |
Hard |
O(N^2) |
O(1) |
Basic |
|
100 |
Easy |
O(N) |
O(N) |
Tree Node |
|
2134 |
Medium |
O(N) |
O(1) |
Sliding Window |
|
1920 |
Easy |
O(N) |
O(1) |
Basic |
|
1480 |
Easy |
O(N) |
O(1) |
Basic |
|
215 |
Medium |
O(N) |
O(1) |
Quick Select |
|
973 |
Medium |
O(N log N) |
O(1) |
Quick Select |
|
15 |
Medium |
O(N^2) |
O(N) |
Two Pointers |
|
8 |
Medium |
O(N) |
O(1) |
Basic |
|
125 |
Easy |
O(log N) |
O(1) |
Two Pointers |
|
9 |
Easy |
O(log N) |
O(1) |
Two Pointers |
|
1496 |
Easy |
O(N) |
O(N) |
HashMap |
|
290 |
Easy |
O(N) |
O(N) |
HashMap |
|
58 |
Easy |
O(1) |
O(N) |
Basic |
|
14 |
Easy |
O(NM) |
O(1) |
Basic |
|
373 |
Medium |
O(N*log N) |
O(N) |
BFS |
|
704 |
Easy |
O(log N) |
O(1) |
Binary Search |
|
733 |
Easy |
O(N*M) |
O(N*M) |
DFS |
Sphinx Python Practice¶
Introduction¶
This repository would be useful as a reference for documentation with Python 3 and Sphinx. There is also a short blog post on this on my website.
It is strongly recommended to watch Brandon Rhodes’s Sphinx tutorial session at PyCon 2013 and leimao’s blog post.
Files¶
.
├── Makefile
├── README.md
├── build
├── doc
│ ├── Makefile
│ ├── README.md
│ ├── __init__.py
│ ├── build
│ ├── make.bat
│ └── source
│ ├── _static
│ ├── _templates
│ ├── api.rst
│ ├── conf.py
│ ├── guide.rst
│ └── index.rst
├── leetcode
│ ├── __init__.py
│ ├── impl
│ │ └── solution.py
│ └── mocktest.py
├── make.bat
├── requirements.txt
└── source
├── _static
├── _templates
├── conf.py
└── index.rst
Installation¶
The leetcode
folder used for the tutorial has no dependency. But for completeness, we added setuptools
and sphinx
to our requirements.txt
.
To install the dependencies, please run the following command in the terminal.
$ pip install -r requirements.txt
To install the leetcode
, which we would not probably do, please run the following command in the terminal.
$ pip install .
Build Documentations¶
Please check the `README
<docs/README.md>`_ in `docs
<docs/>`_ for details.
Tips to Improve Skills¶
- When being stuck, there are a few things you can do to give yourself hints:
Look at the tag for the problem and see what kind of algorithm or data structure is needed for solving
Look at the hints provided by Leetcode
Quickly look at the discussion title to see what the expected complexity is or the algorithm.
Estimate the Time Complexity of the problem based on the input constraints.