Professional Documents
Culture Documents
1 CheatSheet: LeetCode Common Templates & Common Code Problems 1.3 Typical Followup
Interview Num Name Summary
1 From 1-D array to 2-D matrix LeetCode: Number of Submatrices That Sum to Target
• PDF Link: cheatsheet-leetcode-A4.pdf, Category: interview 2 Instead of O(n) space, use O(1) space LeetCode: Find Mode in Binary Search Tree
3 How to do it with multi-threading LeetCode: Web Crawler Multithreaded
• Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-A4
4 Data values have different ranges LeetCode: Find Median from Data Stream
• Related posts: CheatSheet: System Design For Job Interview, #denny-cheatsheets 5 Instead of a fixed list, it’s an ongoing data stream Leetcode: Flatten 2D Vector
https://raw.githubusercontent.com/dennyzhang/cheatsheet.dennyzhang.com/master/cheatsheet-leetcode-A4/datastructre.png
Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-A4 Updated: June 14, 2020 Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-A4 Updated: June 14, 2020
1.12 Top 10 Sliding Window Problems 1.18 Top 5 Montone Stack/Queue Problems
Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-A4 Updated: June 14, 2020 Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-A4 Updated: June 14, 2020
Num Problem Example 1.23 Tips: Think From The Other Direction
1 Longest substring with at most K distinct characters LeetCode: Longest Substring with At Most K Distinct Characters
2 Longest subarray with maximum K 0s LeetCode: Max Consecutive Ones III Num Name Summary
3 Seperate a list into several groups LeetCode: Summary Ranges 1 In graph, instead of deleting edges, add edge in reverse LeetCode: Bricks Falling When Hit
4 Split string LeetCode: License Key Formatting 2 Instead of BFS from empty to islands, do the otherwise LeetCode: As Far from Land as Possible
5 TopK problem LeetCode: Top K Frequent Elements, LeetCode: Find K Pairs with Smallest Sums 3 Treat each point as the last item, instead of the first LeetCode: Burst Balloons
6 Longest Palindromic Subsequence LeetCode: Longest Palindromic Subsequence 4 Avoid deleting element from hashmaps
7 Sort one array based on another array LeetCode: Relative Sort Array
8 Range update with lazy propagation LeetCode: Corporate Flight Bookings 1.24 Common Tips For Clean Code
9 Sliding window with lazy removal Leetcode: Sliding Window Median
10 Get all possibilities of subsets LeetCode: Subsets II, LeetCode: Subsets Num Name Summary
11 Choose k numbers from a list LeetCode: Combination Sum II 1 Calculate sum of a range quickly #presum,LeetCode: Maximum Subarray
12 Combination from multiple segments LeetCode: Letter Combinations of a Phone Number 2 Move in four directions for a matrix LeetCode: Sliding Puzzle
13 Remove nodes from linked list LeetCode: Remove Zero Sum Consecutive Nodes from Linked List 3 Split string by multiple separators LeetCode: Brace Expansion
14 Two pointers LeetCode: Two Sum 4 Add a dummy tailing element to simplify code LeetCode: Brace Expansion
15 Buy stock for maximum profit list LeetCode: Best Time to Buy and Sell Stock 5 Fast slow pointers LintCode: Middle of Linked List
16 Prefix search from a list of strings LeetCode: Longest Word in Dictionary 6 Deep copy an array LeetCode: Combination Sum
17 Factor Combinations LeetCode: Factor Combinations 7 Use arrays instead of hashmaps, if possible LeetCode: Number of Days in a Month
18 Permutation without duplicates LeetCode: Palindrome Permutation II 8 Control the order of dfs LeetCode: Subsets II
19 Convert a number into negative base representation LeetCode: Convert to Base -2 9 Avoid inserting into the head of an array LeetCode: Path In Zigzag Labelled Binary Tree
20 Network connectivity LeetCode: Friend Circles 10 From right to left, instead of left to right LeetCode: Merge Sorted Array
21 Build relationship among different sets LeetCode: Accounts Merge 11 Think the other way around Add Items vs Remove Items, Increase Counter
22 Find the next greater value LeetCode: Daily Temperatures 12 Avoid unnecessary if. . . else. . . res[i] = (diff/2 <= k), LeetCode: Can Make Palin
23 Meeting conflict LeetCode: Meeting Rooms, LeetCode: Course Schedule 13 To get the case of K, solve: at most K - at most (K-1) LeetCode: Subarrays with K Different Integers
24 Minimum conference rooms LeetCode: Meeting Rooms II 14 Instead of deleting entry from hashmap, decrease counter LeetCode: Longest Substring with At Most K Dis
25 Quick slow pointers LintCode: Middle of Linked List 15 Find the max/min; If not found, return 0 LeetCode: Minimum Area Rectangle
26 Longest Repeating Character with at most K changes LeetCode: Longest Repeating Character Replacement 16 With helper function vs without helper function LeetCode: Longest Repeating Character Replacem
27 Prefix and Suffix Search LeetCode: Prefix and Suffix Search 17 Instead of adding a character, try to delete one LeetCode: Longest String Chain
28 Remove duplicate letters LeetCode: Remove Duplicate Letters 18 #roudtrippass: from left to right, then right to left LeetCode: Shortest Distance to a Character
29 Beautiful array LeetCode: Beautiful Array 19 Delayed calculation to simplify the code LeetCode: Interval List Intersections
30 Whether 132 pattern exists in array LeetCode: 132 Pattern 20 Instead of removing, add padding elements LeetCode: Duplicate Zeros
31 Detect conflicts of intervals LeetCode: Non-overlapping Intervals 21 Initialize array with n+1 length to simplify code LeetCode: Range Addition
32 Segment tree: solves range query problems quickly LeetCode: Range Sum Query - Mutable 22 Look for off-by-one errors, sometimes use i+1<len(l) vs i<len(l) LeetCode: Previous Permutation With One Swap
33 Find best meeting points for a list of nodes LeetCode: Best Meeting Point 23 Hashmap can reduce calculation, but may complicate things too LeetCode: Maximum Frequency Stack
34 Find the size of longest wiggle subsequence LeetCode: Wiggle Subsequence 24 Sliding window to get the longest size of subarray LeetCode: Max Consecutive Ones III
35 Sequence reconstruction LeetCode: Sequence Reconstruction 25 In matrix dfs, change cell to impossible value to avoid state hashmap LeetCode: Word Search II
36 Construct Binary Tree from String Construct Binary Tree from String 26 For palindrome check, check the whole string, instead of left half LeetCode: Longest Chunked Palindrome Decomp
37 Use more space to save time LeetCode: Min Stack 27 Use queue to keep flipping the orders LeetCode: Zigzag Iterator
38 Min max game problems LeetCode: Predict the Winner, LeetCode: Stone Game 28 Find a pair with sum meets some requirements LeetCode: Two Sum
39 Shortest Subarray with Sum at Least K LeetCode: Shortest Subarray with Sum at Least K 29 Add a dummy head node for linked list LeetCode: Reverse Linked List
40 Wiggle sort LeetCode: Wiggle Sort II 30 When count sort, use one array instead of two LeetCode: Minimum Number of Steps to Make T
41 Array compressed storage LeetCode: Design Tic-Tac-Toe 31 Hide details which are irrelevant
42 Dead lock: the Dining Philosophers LeetCode: The Dining Philosophers 32 One pass instead of two pass
43 Maintain the order LeetCode: Building H2O 33 Avoid unnecessary precheck
44 Int to string or string to int 34 Reduce search space Leetcode: Bulb Switcher II
45 Expression Add Operators LeetCode: Expression Add Operators
46 Merge k Sorted Lists LeetCode: Merge k Sorted Lists
1.25 More Resources
47 Trapping Rain Water LeetCode: Trapping Rain Water
License: Code is licensed under MIT License.
1.22 Basic Thinking Methodologies https://en.wikipedia.org/wiki/Data_structure
https://www.cs.princeton.edu/~rs/AlgsDS07/
Num Name Summary https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/
1 Trial and error
2 Divide and Conquer
3 Start with naive algorithm, then identify useless steps