Lesson 3
Optimizing Array Analysis with HashMaps
Introduction

Hello there! In this unit, we're offering an engaging coding lesson that highlights the performance efficiencies offered by the utilization of HashMaps in Python. We'll address an array-based problem that requires us to make an optimal choice to minimize the size of our array. Excited? So am I! Let's get started.

Task Statement

In this unit's task, we'll manipulate an array of integers. You are required to construct a Python function titled minimal_max_block(). This function should accept an array as an input and compute an intriguing property related to contiguous blocks within the array.

More specifically, you must select a particular integer, k, from the array. Once you've selected k, the function should remove all occurrences of 'k' from the array, thereby splitting it into several contiguous blocks, or remaining sub-arrays. A unique feature of k is that it is chosen such that the maximum length among these blocks is minimized.

For instance, 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], [5], with the longest containing 6 elements. Now, if we instead remove all instances of 1, the new remaining blocks would be [2, 2, 3], [4, 4, 4], [2, 5], the longest of which contains 3 elements. As such, the function should return 1 in this case, as it leads to a minimal maximum block length.

Brute Force Approach

An initial way to approach this problem can be through a brute force method. Each possible value in the array could be tested in turn, by removing it from the array and then checking the resulting sub-array sizes. This approach entails iteratively stepping through the array for each possible value in the array.

Python
1def minimal_max_block_bruteforce(arr): 2 min_max_block_size = float('inf') 3 min_num = None 4 5 for num in set(arr): # Avoid duplicates. 6 indices = [i for i, x in enumerate(arr) if x == num] # Indices where 'num' appears. 7 indices = [-1] + indices + [len(arr)] # Add artificial indices at the ends. 8 max_block_size = max(indices[i] - indices[i-1] - 1 for i in range(1, len(indices))) # Calculate max block size. 9 10 if max_block_size < min_max_block_size: 11 min_max_block_size = max_block_size 12 min_num = num 13 14 return min_num

This method has a time complexity of O(n2)O(n^2), as it involves two nested loops: the outer loop cycling through each potential k value and the inner loop sweeping through the array for each of these k values.

However, this approach becomes increasingly inefficient as the size n of the array grows, due to its quadratic time complexity. For larger arrays or multiple invocations, the computation time can noticeably increase, demonstrating the need for a more efficient solution.

Complexity Analysis

Before we delve into the steps of the optimized HashMap solution, let's understand why the proposed method is superior in terms of time and space complexity in relation to the brute force approach.

  1. Time Complexity: The HashMap solution only requires a single traversal of the array. This results in a linear time complexity, O(n)O(n), where n is the number of elements in the array. This is significantly more efficient than the O(n2)O(n^2) complexity of the brute force approach, which needs to traverse the array for every distinct number.

  2. Space Complexity: The HashMap solution maintains two dictionaries for storing the last occurrence and maximum block size for each number. In a worst-case scenario, every element in the array is unique, leading to an O(n)O(n) space complexity, where n is the number of elements in the array.

Now let's jump into the step-by-step implementation of the solution.

Step 1: Setup the Solution Approach

In order to find the number that, when removed from the array, would split the array into several contiguous blocks such that the length of the longest block is minimized, we need to track every position of the same elements, the block size when removing each of the encountered elements, and store the maximum of these blocks.

But, in this case, we don't need to store all positions. We only need the last occurrence position since the blocks we are interested in are between two adjacent same elements, the beginning of the array and the first occurrence of an element, and between the last occurrence of an element and the end of the array. For the first two cases, we will keep the maximum block size for each element and update it whenever we get a bigger one, and for the last case, we will process it separately after the array traversal.

To do this, we need to:

  • Initialize two hashmaps. The last_occurrence hashmap will store the last occurrence index of each number, while max_block_sizes will map each number to the maximum size of the blocks formed when the number is removed.

  • Traverse the array from left to right, and for each number:

    • If it's the first time we encounter the number, we regard the block from the start of the array up to its current position as a block formed when this number is removed, and store the size of this block in max_block_sizes for this number.
    • If the number has appeared before, we calculate the size of the block it forms (excluding the number itself) by subtracting the last occurrence index from the current index and subtracting 1. If the size is larger than the current maximum stored in max_block_sizes for this number, we update it.
    • Store the current index as the last occurrence of the number.
  • After finishing the array traversal, we need to calculate the size of the "tail block" (i.e., the block between the last occurrence of a number and the end of the array) for each number, and update its maximum block size in max_block_sizes if necessary.

  • Find the number that gives the smallest maximum block size and return it as the result.

Step 2: Initialize the Dictionaries

First, we initialize two hashmaps. The last_occurrence hashmap stores the last occurrence index of each number, while max_block_sizes maps each number to the maximum size of the blocks formed when the number is removed.

Python
1def minimal_max_block(arr): 2 last_occurrence = {} 3 max_block_sizes = {}
Step 3: Traverse the array

Next, we iterate over the array. For each number:

  • If it's the first time the number is encountered, we regard the block from the start of the array up to its current position as a block formed when this number is removed, and store the size of this block in max_block_sizes for this number.
  • If it has appeared before, we calculate the size of the block it forms by subtracting the last occurrence index from the current index, and subtracting 1 (since block length doesn't include the number itself). We update max_block_sizes for this number if necessary.
  • Store the current index as the last occurrence of this number.
Python
1 for i, num in enumerate(arr): 2 if num not in last_occurrence: 3 max_block_sizes[num] = i 4 else: 5 block_size = i - last_occurrence[num] - 1 6 max_block_sizes[num] = max(max_block_sizes[num], block_size) 7 last_occurrence[num] = i
Step 4: Handle Tail Blocks

Tail blocks are defined as blocks formed from the last occurrence of a number to the end of the array. For each number, we calculate the size of its tail block and update max_block_sizes if necessary.

Python
1 for num, pos in last_occurrence.items(): 2 block_size = len(arr) - pos - 1 3 max_block_sizes[num] = max(max_block_sizes[num], block_size)
Step 5: Return the optimal result

Finally, we find the number associated with the smallest maximum block size in max_block_sizes, and return it.

Python
1 min_num = min(max_block_sizes, key=max_block_sizes.get) 2 3 return min_num
Full code

Putting it all together, here's the full code:

Python
1def minimal_max_block(arr): 2 last_occurrence = {} 3 max_block_sizes = {} 4 5 for i, num in enumerate(arr): 6 if num not in last_occurrence: 7 max_block_sizes[num] = i 8 else: 9 block_size = i - last_occurrence[num] - 1 10 max_block_sizes[num] = max(max_block_sizes[num], block_size) 11 last_occurrence[num] = i 12 13 for num, pos in last_occurrence.items(): 14 block_size = len(arr) - pos - 1 15 max_block_sizes[num] = max(max_block_sizes[num], block_size) 16 17 min_num = min(max_block_sizes, key=max_block_sizes.get) 18 19 return min_num
Lesson Summary

Excellent work! The lesson of this unit was quite comprehensive — we revisited the concept of HashMaps, learned how they enhance the performance of code, and even constructed a function to locate a specific number in an array that minimizes the maximum chunk size upon removal.

Now that we've mastered the basics, the logical next step is to apply your newfound knowledge. In the upcoming practice session, a variety of intriguing challenges await you that delve further into HashMaps, array manipulation, and innovative optimizations. So brace yourself, and let's dive in!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.