Here you can find solutions for various coding/algorithmic problems and many useful resources for learning algorithms and data structures.
Also, this repo will be updated with new solutions from time to time.
Note that this repo is meant to be used for learning and researching purposes only and it is not meant to be used for production.
All solutions are written in Python (more precisely, Python 3), using the Built-in Functions (print, len, range, sorted, sum, min, max, etc...) and a few modules from the Python Standard Library like:
- math (used for constants like math.pi, math.inf and functions like math.ceil, math.floor, math.gcd, math.log, math.pow, math.sqrt, etc)
- collections (used for collections.deque when there is a need for Stack or Queue data structures)
- heapq (used when there is a need for Priority Queue data structure).
- random (used for nondeterministic algorithms, like shuffling arrays).
So, to execute these solutions there is no need from installing any external packages.
Coding style and name conventions are described in the official PEP8 page.
Note that I'm not the author of these problems, they are from sites like LeetCode (you can find more than 40 sites like this in the Training Sites section). Only the solutions and explanations are mine.
For easier navigation into the solutions, each file with a solution in this repo will have the following template:
'''
Problem Name
Problem explanation.
Input: XXX
Output: XXX
Output explanation: XXX
=========================================
Solution 1 explanation.
Time Complexity: O(X)
Space Complexity: O(X)
Solution 2 explanation.
(some of the problems are solved in more than one way)
Time Complexity: O(X)
Space Complexity: O(X)
'''
##############
# Solution 1 #
##############
def name_of_solution_1(params):
# description of code
pass
##############
# Solution 2 #
##############
def name_of_solution_2(params):
# description of code
pass
###########
# Testing #
###########
# Test 1
# Correct result => 'result1'
test_val = 'example1'
print(name_of_solution_1(test_val))
print(name_of_solution_2(test_val))
# Test 2
# Correct result => 'result2'
test_val = 'example2'
print(name_of_solution_1(test_val))
print(name_of_solution_2(test_val))
Each solution/problem in this repo belongs to one of these categories:
- Arrays - Array Manipulations, Sorting, Binary Search, Divide and Conquer, Sliding Window, etc.
- Linked Lists - Linked List Searching, Pointer Manipulations, etc.
- Trees - Binary Search Trees, Tree Traversals: Breadth-First (Level Order) Traversal, Depth-First Traversal (Inorder, Preorder, Postorder), etc.
- Hashing DS - Hashing Data Structures: Sets/HashSets and Dictionaries/HashMaps.
- Dynamic Programming - 2D and 1D Dynamic Programming, LCS, LIS, Knapsack, etc.
- Strings - String Manipulations, Reversing, Encodings/Decodings, etc.
- Math - GCD, LCM, Factorization, Geometry, Math Formulas, etc.
- Other - Backtracking, BFS, DFS, Stacks, Queues, Deques, Priority Queues (Heaps), Matrices, etc.
The learning resources are divided into 4 categories: Courses, Books, Training Sites, Other Resources.
Collection of free courses from one of the best CS universities.
-
Standford University
-
Princeton University (Coursera)
-
MIT University (YouTube)
- Introduction to algorithms 2005 - Official MIT page with resources. Note: this course is the old 6.046J course (the new name is Design and analysis of algorithms, you can find it below).
- Introduction to algorithms 2011 - 6.006
- Design and analysis of algorithms - 6.046J - Official MIT page with resources
- Advanced Data Structures - 6.851 - Official MIT page with resources
- Advanced Algorithms 2016 - 6.854
-
Harvard University (YouTube)
-
UC Berkeley
Several books that have made an impression on me:
- Grokking Algorithms by Aditya Bhargava - The best book for complete beginners in algorithms! I wish this book existed when I started learning algorithms.
- Introduction to Algorithms by CLRS - This book is called "bible textbook of algorithms" by many programmers.
- Algorithms by Robert Sedgewick & Kevin Wayne - These authors are instructors of the previously mentioned coursera courses: Algorithms Part 1 and Algorithms Part 2. Also this book has excellent and free site with exercises, presentations, and examples.
- The Algorithm Design Manual by Steven Skiena - The book describes many advanced topics and algorithms and it focuses on real life practical examples. This book has one of the best site with resources (solutions, algorithms and data structures, python implementations).
- Algorithms by S. Dasgupta, C. Papadimitriou, and U. Vazirani - This book is an official book for algorithms and data structures classes in several famous universities.
- Competitive Programming 3 by Steven Halim & Felix Halim - A great book that prepares you for competitive programming (not for complete beginners). You can learn many things and tricks about competitive programming. But if your goal is to prepare for competitive programming then choose a faster language than Python, C/C++ (or Java, it's faster than Python but not like C/C++).
If the problems from LeetCode are not enough and you need more problems like those, you can find much more on these platforms:
- HackerRank
- CodeChef
- HackerEarth
- CodeForces
- Topcoder
- Project Euler
- SPOJ
- A2OJ
- PEG
- Online Judge
- E-Olymp
- VJudge
- USA CO
- CodeAbbey
- CS Academy
- CodingBat
- AtCoder
- Russian Code Cup
- Wolfram Challenges
- Google's Coding Competitions
- Codility
- CoderByte
- Rosetta Code
- Daily Coding Problem
- Daily Interview Pro
- Codewars
- LintCode
- DevPost
- Exercism
- CodeFu
- Mendo
- Z-Training
- Edabit
- Cyber-dojo
- CodeKata
- Advent of Code
- Kattis
- Brilliant
- AlgoExpert
- Codingame
- CheckiO
- FightCode
- Kaggle
- Rosalind
- Geeks For Geeks - The site which all interested in algorithms (no matter if beginners or experts) should know! YouTube channel with many useful videos.
- The Algorithms - Python - Great GitHub repo with many algorithms written in Python (Link from the same repo written in other programming languages).
- CP Algorithms - Great page with excellent explanations for various algorithms.
- KhanAcademy - Algorithms - Good explanations for some basic algorithms.
- HackerEarth - Tutorials and Practice - Practice problems and learn about many algorithms and data structures needed for competitive programming.
- HackerRank - YouTube tutorials