Skip to content

zhenyulin/py-neetcode

Repository files navigation

Py-Neetcode

algorithm cheat sheet in Python summarised by data types

Data Structure Implementations

Algorithms by Data Structure

Int

Greedy


String

Math Calculation O(1) time

count combination

Greedy O(N) time - rolling update with condition

substring(shortest, longest, len, min, max) -> greedy, sliding window

Greedy & Hashmap O(C) space - index/count related condition

string(index, count, last, match) -> Hashmap

1D Dynamic Programming O(N) time, O(1 or N) space - multiple relations to between n-x and n

subsequence, combination(count, match, min, max) -> dynamic programming

2D Dynamic Programming O(M*N) time, O(M or N or M*N) space - multiple relations and multiple strings

two subsequences, subsequence match -> 2d dynamic programming

Backtracking or DFS O(N*O^L) time - conditional generation or search

combination(generate) -> backtracking, combination(dictionary) -> DFS


1D Array

Math Calculation O(1) time

Binary Search O(LogN) time - search until match or left-right condition

sorted search, search peak -> binary search

Binary Search + Greedy

Greedy O(N) time - rolling update on condition

subarray(max, min, longest, except self) -> greedy

Greedy + Sliding/Two-Pointer Window O(1) space - forward and backward

subarray(area, remove, longest, shortest) -> sliding window

Greedy + Implicit BFS

Greedy & Hashmap O(N) space - index/count related condition

array(index, count, last) -> Hashmap

Greedy & Stack O(N) time, O(K) space

stream sliding window max, rectangle area -> stack

Greedy & Heap O(K*LogN or N*LogK) time, O(N) space - rank, sort

stream k, kth, median, subgroups consecutive -> heap

Greedy & Combinations O(N^2) time

1D Dynamic Programming O(N) time, O(N or 1) space - multiple relations to between n-x and n

subsequence or combination(fewest, sum to, longest) -> dynamic programming

1D Dynamic Programming + Cached DFS

2D Dynamic Programming

subsequence(average)

Backtracking, DFS O(N*O^L) time - conditional combinations

combination(generate) -> backtracking


2D Array

Binary Search O(LogM*LogN) time

sorted search, search peak -> binary search

Greedy & Set O(M*N) time, O(1) space

BFS O(M*N) time, O(M*N) space - shortest path

shortest path, shortest distance, edge areas -> BFS

2D Dynamic Programming or cached DFS O(M*N) time, O(M*N) space

count longest path

DFS O(M*N or M*N*O^L) time, O(O^L) space - known target

longest path, target sequence, edge areas -> DFS


Bit

n << 1 <=> n * 2 n >> 1 <=> n // 2 n & 1 <=> n % 2

Bit Iteration, Shift O(1) time

range(32) or range(31, -1, -1)

As an Array

bit base(conversion, calculation)


Tree

Recursion O(N) time, O(N) space

BFS O(N) time, O(N) space

DFS or Backtracking O(N) time, O(N) space


Linked List

Simple Forward O(N) time, O(1) space

Fast Slow Pointers O(N) time, O(1) space


Graph

DSU(Root Union) O(C) time, O(N) space {node: root}

undirected set

Topological Sort O(C) time, O(C+N) space {up: [downs]}, [up_counts]

directed cycle, paths

DFS O(N^2) time {node: [connection]}

matrix count groups


Geometry

Triangle

About

neat solutions for selected leetcode problems

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published