Reached 500 problems in Leetcode
I have reached 500 problems in Leetcode.
- Now, I am solving easy problems within about 5-10 minutes.
- Medium ones can take up to 30-45 minutes. Sometimes, I can't solve, and will learn from the solution.
- Hard ones depend on the question itself, 1-2 hours, or forever until I take a look at the solution.
Below I am providing the patterns that I observed during this journey.
- HashMaps - powerful data structure to store key value pairs. Used for problems where you need to remember some previous values or computation results.
- Two pointers - is a simple, yet powerful technique where you traverse the array (or any linear data structure) using two pointers. Pointers can go in one direction or go to each other.
- Sliding Window - is a technique to solve some sort of problems with O(n) time complexity instead of O(n^2). Basic idea is to initialize a window (with start and end indices in array), and you move the window along the array and solve the problem depending on the window validity.
- Stacks - LIFO data structure, meaning the item inserted last will be popped first. Appropriate for problems like "valid parenthesis". Also, we have monotonic stacks which can be used to find nearest greater or less values for each index of an array.
- Queues - FIFO data structure, meaning the item inserted first will be popped first. Queues are widely used in graph traversals (aka BFS).
- LinkedList - Linear data structure to store elements. Adding elements is O(1) where reading is O(n). Whereas, in arrays, adding elements is O(n) and reading is O(1). So, LinkedList is more appropriate for more appends and fewer reads, and arrays are appropriate for fewer appends and more reads.
- Slow-fast pointers - are a good technique used in LinkedList to detect cycles. Also, can be used to find the length of an acyclic LinkedList.
- Searching - there are many types of searching in arrays. If the array is sorted, then definitely binary search is the best with O(log n). If the array is unsorted, then searching should be linear with O(n).
- Sorting - there is a trade off with sorting. The fastest algorithms (ex: merge sort, quick sort) sort the array with O(n log n) time complexity, but require O(n log n) memory. Algorithms like bubble sort, selection sort are slower with O(n^2) time complexity, but do not require any extra memory.
- Binary Tree - binary tree is a specific data structure where there is a root node. And, each node can contain left and right sub-nodes. Traversing the tree is O(n) where n is the number of nodes. BFS and DFS can be used to traverse them. Also, pre-order, in-order, and post-order DFS traversals are commonly used in some problems.
- Binary search tree - is a specific binary tree, but with one rule. When inserting an element to BST, if the element is greater than the node (root or below nodes) it goes to the right side, otherwise to the left side. This rule is good when searching an element from BST. The time complexity reduces to O(log n) from O(n).
- Backtracking - I call it a brute-force technique. Basically, it is all about checking all the possible paths or ways to solve the problem. Think you are in a labyrinth. And your goal is to find the exit. You start from the entrance, and you explore all the paths. When you hit a wall, you go back and explore another path and so on..
- Heap (aka Priority Queue) - is a powerful data structure to access the min or max of inserted elements with constant time. Think with this problem. You are given an array, and you need to find k greatest elements. Brute force approach might be iterating over the array k times, and removing/inserting the greatest element to the result. Time complexity will be O(k×n). But with heaps, it can be reduced. You can heapify array, meaning you insert all elements to heap, then k times you can get the elements from heap. Time complexity will be O(k + n×log n). If you use a specific technique called min-heap, the time complexity further reduces. Here, you can maintain a min-heap with k size, and you can keep adding elements to it. If the size exceeds k, then you can remove the smallest one with O(1). Time complexity will be O(k×log n + n).
- Graph - is a data structure which contains nodes. And, nodes are connected with edges. Graphs can be directed or undirected. Directed graphs contain directed edges, meaning if you can access node A from node B, you cannot access node B from node A if not permitted explicitly. In undirected graphs, any two connected nodes can reach each other. BFS and DFS are commonly used techniques to traverse the graphs. There are many algorithms with graphs, and below I am listing some of them.
- Union Find - is an algorithm to find connected components in a graph.
- Dijkstra's algorithm - is used to find minimum distance between two nodes in a graph.
- MST - Minimum Spanning Tree is a specific graph that does not contain any cycles and uses minimum distances to connect nodes in the graph. Prim's and Kruskal's algorithms can be used to find MST in a graph.
- Topological sort - is a sorting of nodes of a directed graph, where outermost nodes (which have no incoming edges) come first. Best example might be course scheduling problem.
- Dynamic programming - it is all about finding subproblems and storing their results in a storage for later usage, and solving big problems using those subproblems.
- Prefix sum - used in arrays. In some problems, you need to precompute some computations. Ex: Find left max for each index. Naive approach would be for each index, checking all nums from 0 until index. Time complexity will be O(n^2), but with prefix sum that can be with O(n)
- Segment Tree - is a specific data structure used to efficiently query ranges in an array. For example, finding max in specific range.
- Trie - is a specific tree data structure used to search words from specific dictionary. You can research more on this.