LeetCode solutions.
Always try to optimize time complexity O(n^2) -> O(nlogn) or O(n) in most cases.
Python Tips:
- Dictionary build-in method keys() will not return a list object. To get a list of keys, need try list(newdict.keys()). This will convert the dict_keys object to a list.
- Dictionary (Hash Table) should be considered in priority to deal with the duplicated information or the mapping relationship.
- For Linked List problem, using/creating a dummy head is usually needed.
- For Linked List problem, the middle element of a list can be found by using a slow and a fast pointers together.
Java Tips:
- The ASCII values of '`' = 96, 'a' = 97, 'z' = 122, '{' = 123. The full ASCII table can be found at: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Data Structure Implementation:
- An implementation of Trie can be found in 720. Longest Word in Dictionary
- An implementation of Max Heap can be found in 692. Top K Frequent Words
Array Problems:
- Using 2 pointers.
- Or using Priority Queue (Heap) if some kinds of ordering is required.
- Regarding circular array, possible solution is: -- concatenate the array with its copy to get a new array with double length. But cost extra space. -- Or, use larger index range like 0 < i < 2n with mod operation i%n, here the n is the length of array.
List Problems:
- Using two pointers (slow pointer and fast pointer) to locate the middle element of the list.
Memoization:
- For a tree-like data structure, probably need a Hash table such as HashMap<TreeNode, Integer> to store the calculated values for Java implementation.
Sorting:
- If the number of data is not big and related to integer, could using Counting Sort for better Time Complexity.
- TreeMap is a choice when handle sorting of large number data.
Binary Search:
- Use 'index space' as searching space in 1-dimension data like array
- Use 'number range space' as searching space in 2-dimension data like matrix
Dynamic Programming:
- the Math.max() and Math.min() are often used in Java solutions
- a local extremum (optimal) value is always used
- a matrix dp[i][j] is used to store result in most cases
Bit Manipulation:
Use Column Operation
Interval Problem: Use TreeMap with its lowerKey() or floorKey() and higherKey() or ceilingKey()
Using HashMap to reduce Time Complexity:
0560. Subarray Sum Equals K
Sort Time Stamp with TreeMap:
0253. Meeting Rooms II
0731. My Calendar II
1094. Car Pooling
Boyer-Moore Majority Vote algorithm
0229. Majority Element II
Morris Traversal 0099. Recover Binary Search Tree
Good discussion and explanation:
- Approach the problem using the "trial and error" algorithm:
https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/109082/Approach-the-problem-using-the-%22trial-and-error%22-algorithm - Summary of solutions for problems "reducible" to LeetCode 378:
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/115819/Summary-of-solutions-for-problems-%22reducible%22-to-LeetCode-378
Heuristic Solutions:
- Longest Common Prefix
- Maximum Subarray
- Best Time to Buy and Sell Stock III
- Linked List Cycle
- Linked List Cycle II
- Intersection of Two Linked Lists
- Majority Element
- Palindrome Linked List
- Range Sum Query 2D - Immutable