Posts

Code templates

Here are code templates for common patterns for all the data structures and algorithms looked at in LICC . Two pointers: one input, opposite ends Two pointers: two inputs, exhaust both Sliding window Build a prefix sum Efficient string building In JavaScript, benchmarking shows that concatenation with += is faster than using .join() . Linked list: fast and slow pointer Reversing a linked list Find number of subarrays that fit an exact criteria Monotonic increasing stack The same logic can be applied to maintain a monotonic queue. Binary tree: DFS (recursive) Binary tree: DFS (iterative) Binary tree: BFS Graph: DFS (recursive) For the graph templates, assume the nodes are numbered from 0 to n - 1 and the graph is given as an adjacency list. Depending on the problem, you may need to convert the input into an equivalent adjacency list before using the templates. Graph: DFS (iterative) Graph: BFS Find top k elements wit...

Sliding window problem with 2 pointer

  Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t ( including duplicates ) is included in the window . If there is no such substring, return the empty string "" . The testcases will be generated such that the answer is unique .   Example 1: Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. Example 2: Input: s = "a", t = "a" Output: "a" Explanation: The entire string s is the minimum window. Example 3: Input: s = "a", t = "aa" Output: "" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.   Constraints: m == s.length n == t.length 1 <= m, n <= 10...

Heap (min)

 Heap Heap is a data structure used to store numbers in form of binary tree i.e each node has two children. Heap is the data structure behind PriorityQueue. Insert and pop operation inside a heap is oh log(n) complexity and can be used in multiple insert and pop operation questions. It is efficient and used in fastest sorting as well as sorting only takes nlog(n) complexity at worst case. Important Functions for heap: 1.  Heapify: This function is a top down approach to set an given array into heap. The idea is to set the top priority Item at the parent node of given subtree of 3. We will compare priority of left item from parent and then from right item to that of winner of parent and left and then swap the top priority with parent item. If top priority wasn't the parent item then we call the heapify again for the right or left node with which the value was swapped. If we have a given array then we can run a loop into that array from bottom of tree to top (excluding leafs),i....

1631. Path With Minimum Effort

Image
  You are a hiker preparing for an upcoming hike. You are given   heights , a 2D array of size   rows x columns , where   heights[row][col]   represents the height of cell   (row, col) . You are situated in the top-left cell,   (0, 0) , and you hope to travel to the bottom-right cell,   (rows-1, columns-1)   (i.e.,  0-indexed ). You can move   up ,   down ,   left , or   right , and you wish to find a route that requires the minimum   effort . A route's  effort  is the  maximum absolute difference   in heights between two consecutive cells of the route. Return  the minimum  effort  required to travel from the top-left cell to the bottom-right cell. Example 1: Input: heights = [[1,2,2],[3,8,2],[5,3,5]] Output: 2 Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells. This is better than the route of [1,2,2,2,5], where the maximum abso...