Posts

Showing posts from June, 2022

Understand pattern to solve coding questions

  Instead of learning solutions to LeetCode questions, understand patterns!! If the input array is sorted -binary search -two pointers If asked for all permutations/subsets then -Backtracking If given a tree/Graph then -DFS -BFS If given a linked list then -two pointers If recursion is banned -stack If must solve in-place then -swap corresponding values -store one or more different values in the same pointer If asked for maximum/minimum -subarray/subset/options then -dynamic programming If asked for top/least K items then -Heap If asked for common strings then -map -trie else -map/set for O(1) time & O(n) space -sort input for O(nlogn) time and O(1) SPACE

Understand pattern to solve coding questions

Instead of learning solutions to LeetCode questions, understand patterns!! If the input array is sorted -binary search -two pointers If asked for all permutations/subsets then -Backtracking If given a tree/Graph then -DFS -BFS If given a linked list then -two pointers If recursion is banned -stack If must solve in-place then -swap corresponding values -store one or more different values in the same pointer If asked for maximum/minimum -subarray/subset/options then -dynamic programming If asked for top/least K items then -Heap If asked for common strings then -map -trie else -map/set for O(1) time & O(n) space -sort input for O(nlogn) time and O(1) SPACE