Welcome! In this lesson, we're going to explore the performance efficiencies offered by utilizing Ruby's Hash
. We will tackle an array-based problem requiring us to choose an optimal strategy to minimize the size of our array. Excited to dive in? Let's get started!
Our task is to manipulate an array of integers. You are required to construct a Ruby method titled minimal_max_block
. This method should accept an array as input and examine an interesting property related to contiguous blocks within that array.
Specifically, you have to select a particular integer, k
, from the array. Once you've selected k
, the method should remove all occurrences of k
from the array, resulting in multiple contiguous blocks, or sub-arrays. The unique feature of k
is that it is chosen such that the maximum length among these blocks is minimized.
For example, consider the array [1, 2, 2, 3, 1, 4, 4, 4, 1, 2, 5]
. If we eliminate all instances of 2
(our k
), the remaining blocks would be [1]
, [3, 1, 4, 4, 4, 1]
, , with the longest containing 6 elements. Now, if we instead remove all instances of , the new remaining blocks would be , , , with the longest containing 3 elements. Hence, the method should return in this case, as it leads to a minimal maximum block length.
