Design Memory Allocator - Complete Solution Guide
Design Memory Allocator is LeetCode problem 2502, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer n representing the size of a 0-indexed memory array. All memory units are initially free. You have a memory allocator with the following functionalities: Allocate a block of size consecutive free memory units and assign it the id mID . Free all memory units with the given id mID . Note that: Multiple blocks can be allocated to the same mID . You should free all the memory units with mID , even if they were allocated in different blocks. Implement the Allocator class: All
Detailed Explanation
The problem asks us to implement a memory allocator that manages a contiguous block of memory. The allocator should support two operations: `allocate` and `freeMemory`. `allocate(size, mID)` finds the leftmost block of `size` consecutive free memory units and assigns the ID `mID` to it, returning the starting index of the allocated block. If no such block exists, it returns -1. `freeMemory(mID)` frees all memory units with the given ID `mID` and returns the number of units freed. The memory is represented as an array, initially all zeros, indicating free memory. We need to design a class with these functionalities, handling constraints on the size of the memory and the number of operations.
Solution Approach
The provided solution uses a simple array-based approach to simulate the memory allocator. The `Allocator` class maintains an integer array `memory` to represent the memory blocks. The `allocate` method iterates through the `memory` array, keeping track of the consecutive free blocks. If it finds a block of sufficient size, it assigns the given `mID` to the block and returns the starting index. The `freeMemory` method iterates through the `memory` array, freeing all blocks with the given `mID` and counting the number of freed blocks.
Step-by-Step Algorithm
- Step 1: Initialize the `Allocator` class with the size `n` and create a `memory` array of size `n` filled with zeros.
- Step 2: In the `allocate` method, iterate through the `memory` array.
- Step 3: Maintain a `free_count` variable to track the number of consecutive free blocks.
- Step 4: If `memory[i]` is 0, increment `free_count`. Otherwise, reset `free_count` to 0.
- Step 5: If `free_count` equals the requested `size`, calculate the starting index of the block (`start_index = i - size + 1`).
- Step 6: Assign the given `mID` to the memory blocks from `start_index` to `start_index + size - 1`.
- Step 7: Return `start_index`.
- Step 8: If the loop completes without finding a suitable block, return -1.
- Step 9: In the `freeMemory` method, iterate through the `memory` array.
- Step 10: If `memory[i]` equals the given `mID`, set `memory[i]` to 0 and increment a `freed_count` variable.
- Step 11: Return `freed_count`.
Key Insights
- Insight 1: The core idea is to simulate the memory allocation process using an array to represent the memory blocks and their respective IDs.
- Insight 2: The `allocate` operation requires finding a contiguous block of free memory, which can be done by iterating through the array and keeping track of the current free count.
- Insight 3: The `freeMemory` operation requires iterating through the entire array to find and free blocks associated with the specified ID.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Design, Simulation.
Companies
Asked at: ByteDance, OpenAI, Qualcomm, Rubrik, Two Sigma.