Squares of a Sorted Array
Return the squares of a sorted array in sorted order using two pointers from both ends, in O(n) time.
Arrays, Strings, Sliding Window, Two Pointers, Searching & Binary Search, Sorting, Linked List, Stack & Queue, Recursion & Backtracking, Trees, Heap / Priority Queue, Graphs, Dynamic Programming, Greedy, Bit Manipulation, Trie, Math & Number Theory, Design (LLD / OOP), SQL, System Design (HLD)
180 notes in 20 topics
Return the squares of a sorted array in sorted order using two pointers from both ends, in O(n) time.
Merge two sorted arrays in place using two pointers from the back, in O(m+n) time and O(1) space.
Find the intersection of two arrays including duplicates using sorting and two pointers, in O(n log n) time.
Compare two strings containing backspace characters using two pointers scanning from the end, in O(n) time and O(1) space.
Compute how much rain water is trapped between bars using the two pointer technique with running max heights, in O(n) time and O(1) space.
Find two lines that hold the most water using two pointers from both ends, moving the shorter line inward, in O(n) time.
Partition a string into as many parts as possible so each letter appears in one part, using last-occurrence indices and an expanding window, in O(n) t...
Sort an array of 0s, 1s, and 2s in a single pass using the Dutch National Flag three pointer technique, in O(n) time and O(1) space.
Remove duplicates from a sorted array so each value appears at most twice, in place using the slow/fast two pointer pattern, in O(n) time.
Detect a cycle in a linked list using Floyd’s fast and slow pointer algorithm, in O(n) time and O(1) space.
Find the middle node of a linked list in one pass using fast and slow pointers, in O(n) time and O(1) space.
Check if a linked list is a palindrome by finding the middle with fast/slow pointers, reversing the second half, and comparing, in O(n) time and O(1)...