K Empty Slots

Given N flowers. One flower blooms per day. Array flowers[i] gives the flower position (1-based) blooms at i + 1th day. Find the first day where there are k not bloomed flowers between two flowers already bloomed. [Optimal]Two Pointers/Sliding Window Solution Put in another word, we need to find two flowers such that all the […]

Degree of an Array

Degree of an Array The degree of an array is defined as the number of occurrence of the most elements in an array. The problem is related to find the shortest subarray that contains elements with that degree. This solution builds a map from elements to their count, meanwhile find all the elements with the […]

String Compression

Given a string, compress the string into “char” and “count”. i.e. [“a”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”] gets converted into [“a”,”b”,”1″,”2″]. The key observation here is at any given time in point, the compressed string’s length will be equal or less than the original string, so we can override values in the original array. We need to maintain a char […]