Find K Closest Elements — Binary Search on Window Boundary
LeetCode 658 asked at Google, Amazon, and Microsoft. Binary search on the window left boundary returns the k closest elements in O(log(n-k)+k) time.
1582 articles
LeetCode 658 asked at Google, Amazon, and Microsoft. Binary search on the window left boundary returns the k closest elements in O(log(n-k)+k) time.
LeetCode 2444 asked at Google and Amazon. Track last-bad, last-minK, and last-maxK indices to count valid subarrays in a single O(n) pass.
LeetCode 487 asked at Microsoft, Meta, and Amazon. Track the last zero index instead of a counter to handle the streaming follow-up in O(n) time and O(1) space.
LeetCode 2785 asked at Amazon and Microsoft. Extract vowels, sort by ASCII, then reinsert at original positions in O(n log n) time and O(n) space.
LeetCode 727 asked at Google and Amazon. A forward scan finds a valid right boundary, then a backward scan tightens the left boundary in O(|s|*|t|) time.