Third Maximum Number — OrderedSet O(n) [Microsoft Easy]
Return the third distinct maximum or the max if fewer than 3 distinct values exist. Three-variable tracking O(n) O(1) solution.
webcoderspeed.com
20 articles
Return the third distinct maximum or the max if fewer than 3 distinct values exist. Three-variable tracking O(n) O(1) solution.
Find all unique triplets summing to zero. Sort array then fix one element and use two pointers. Full O(n²) solution in C, C++, Java, JavaScript and Python.
Merge all overlapping intervals. Sort by start, then merge greedily in one pass. O(n log n) time.
Find k most frequent elements. Approach 1: min-heap O(n log k). Approach 2: bucket sort O(n). Full 5-language solutions.
Rearrange an array so nums[0] < nums[1] > nums[2] < nums[3]... using virtual index mapping and nth_element for O(n) time.
Rearrange nums to maximize advantage over B using a greedy strategy: assign the smallest winning card, else discard the smallest.
Find the shortest subarray that when sorted makes the whole array sorted, using a single linear scan tracking violated boundaries.
Count spell-potion pairs with product >= success by sorting potions and binary searching for each spell's threshold.
Find the maximum number of nested envelopes by sorting by width ascending then height descending, then applying LIS.
Assign gold/silver/bronze or rank numbers to athletes based on their scores using sorted ordering.
For each query point, find the smallest interval containing it by sorting both intervals and queries, using a min-heap.
Sort array elements by frequency ascending, breaking ties by value descending.
Find the kth largest level sum in a binary tree using BFS to compute level sums then sorting or using a heap.
Group tree nodes by vertical column then row, sorting by value within same position using BFS with coordinates.
Find minimum swaps to sort each level of a binary tree using BFS and cycle-detection in permutation sorting.
Find all unique triplets summing to zero by sorting and using two pointers for each fixed element with careful duplicate skipping.
Find the maximum frequency of any element after at most k increments by sorting and using a sliding window with a running sum.
Find the minimum difference between the max and min of any k scores by sorting and using a fixed window of size k.
Build a suffix array in O(n log²n) using doubling sort. Enables O(m log n) substring search, longest common substring, and number of distinct substrings.
Design an in-memory key-value database that supports set, get, delete, and rank operations. Demonstrates combining hashmaps with sorted structures for efficient multi-key queries.