Kth Largest Element in an Array
Push each number into a heap, trim the heap whenever it grows beyond size k, and the root will end up as the kth largest value.
Return the kth largest element from an unsorted list.
All revision cards
Push each number into a heap, trim the heap whenever it grows beyond size k, and the root will end up as the kth largest value.
Return the kth largest element from an unsorted list.
Shrink the candidate prefix until every string starts with it. If it becomes empty, there is no shared prefix.
Return the longest common prefix shared by all strings in a list.
Put all numbers in a set, then expand sequences only from values whose previous number is absent. That avoids re-counting the same streak multiple times.
Return the length of the longest consecutive sequence in an unsorted array.
Expand the right edge one character at a time and move the left edge forward whenever a duplicate falls inside the current window. Track the maximum window length as you go.
Find the length of the longest substring that contains no repeated characters.
Store items in an OrderedDict, move keys to the end whenever they are read or updated, and evict the oldest item when capacity is exceeded.
Implement an LRU cache with O(1) get and put operations.
Track a running candidate and increment or decrement a counter as you scan. When the counter drops to zero, start fresh with the current value as the new candidate.
Return the element that appears more than n // 2 times in an array.
Track the best subarray ending at the current position and reset only when starting fresh is better than extending the previous run.
Return the largest possible sum of a contiguous subarray.
Sort intervals by start, then compare each interval with the current merged tail. Extend the tail end when ranges overlap; otherwise append a new interval.
Merge all overlapping intervals and return the condensed list.
Walk both arrays with two pointers, append the smaller value each step, and then append the remaining tail from whichever array is not exhausted yet.
Merge two sorted arrays into one sorted list.
Use a dummy head and repeatedly attach the smaller current node from either list. When one list runs out, append the remaining nodes from the other list.
Merge two sorted linked lists and return the head of the merged list.