Hello there! Welcome to this exciting TypeScript coding lesson, where we'll explore the performance efficiencies gained by utilizing Map
types. Our focus will be on solving a problem involving arrays, where we aim to make an optimal choice to minimize the size of segments. Ready to dive in? Let's get started!
In this unit's task, we're tasked with creating a TypeScript function named minimalMaxBlock()
. This function should accept an array of integers and compute an interesting property related to contiguous segments within the array.
Specifically, you'll need to select a particular integer, k
, from the array. By choosing k
, the function will remove all occurrences of k
from the array, splitting it into several contiguous segments. The unique aspect of k
is that it should be chosen such that the maximum length among these segments is minimized.
For example, with the list [1, 2, 2, 3, 1, 4, 4, 4, 1, 2, 5]
, removing all instances of 2 results in blocks [1]
, [3, 1, 4, 4, 4, 1]
, [5]
, with the longest containing six elements. However, if we remove all instances of 1, the remaining blocks are , , , with the longest containing three elements. Therefore, the function should return 1, minimizing the maximal block length.
