Sort Colors [Medium] — Dutch National Flag Algorithm
Sort an array of 0s, 1s, and 2s in-place in one pass using Dijkstra's Dutch National Flag algorithm.
webcoderspeed.com
27 articles
Sort an array of 0s, 1s, and 2s in-place in one pass using Dijkstra's Dutch National Flag algorithm.
Generate the power set of distinct integers using backtracking or bitmask enumeration.
Determine if there exists an increasing subsequence of length 3 using two greedy variables in O(n) time O(1) space.
Find the longest consecutive integer sequence in O(n) using a HashSet and only extending sequences from their start.
Calculate minimum CPU intervals for tasks with cooldown using a greedy formula based on the most frequent task count.
Maximize a number by making at most one swap, using a last-occurrence map to find the optimal swap greedily.
Find a peak element in O(log n) by binary searching on the slope direction — always move toward the higher neighbor.
Calculate trapped rain water using inward two pointers that track left-max and right-max, replacing the classic O(n) space approach.
Find the minimum window in s that contains all characters of t using a shrinkable sliding window with a character frequency counter.
Find the median of two sorted arrays in O(log(min(m,n))) by binary searching for the correct partition point.
Find two indices that add to target in O(n) by storing each number in a HashMap and looking up the complement.
Group strings that are anagrams together by using their sorted form as a HashMap key.
Find the k most frequent elements in O(n) using bucket sort by frequency instead of a heap.
Design a Least Recently Used cache with O(1) get and put operations using a HashMap combined with a doubly linked list.
Count subarrays with sum exactly k using prefix sums stored in a HashMap to find valid starting positions in O(n).
Find the longest consecutive integer sequence in O(n) by using a HashSet and only starting sequences from their minimum element.
Design a set with O(1) insert, delete, and getRandom using a dynamic array combined with a HashMap for index tracking.
Remove duplicates in-place from a sorted array using a write pointer that only advances on unique elements.
Find the longest substring without duplicate characters using a HashSet-backed shrinkable sliding window.
Maximize water trapped between two vertical lines using inward two pointers that always move the shorter line inward.
Find all unique triplets summing to zero by sorting and using two pointers for each fixed element with careful duplicate skipping.
Find the maximum consecutive ones in a binary array when you can flip at most k zeros, identical to the Max Consecutive Ones III pattern.
Find the smallest window in s containing all characters of t using a have/need counter to track when the window is valid.
Find the longest substring containing at most 2 distinct characters using a variable sliding window with a character count map.
Sort an array of 0s, 1s, and 2s in one pass using the Dutch National Flag 3-pointer algorithm.
Check if a string can become a palindrome by deleting at most one character using recursive two-pointer palindrome checking.
Calculate trapped rainwater using optimal two-pointer approach that tracks left and right maximums without extra space.