2502. 设计内存分配器

题目描述:

给你一个整数 n ,表示下标从 0 开始的内存数组的大小。所有内存单元开始都是空闲的。

请你设计一个具备以下功能的内存分配器:

分配 一块大小为 size 的连续空闲内存单元并赋 id mID 。
释放 给定 id mID 对应的所有内存单元。
注意:

多个块可以被分配到同一个 mID 。
你必须释放 mID 对应的所有内存单元,即便这些内存单元被分配在不同的块中。
实现 Allocator 类:

Allocator(int n) 使用一个大小为 n 的内存数组初始化 Allocator 对象。
int allocate(int size, int mID) 找出大小为 size 个连续空闲内存单元且位于 最左侧 的块,分配并赋 id mID 。返回块的第一个下标。如果不存在这样的块,返回 -1 。
int free(int mID) 释放 id mID 对应的所有内存单元。返回释放的内存单元数目。

示例:

输入
[“Allocator”, “allocate”, “allocate”, “allocate”, “free”, “allocate”, “allocate”, “allocate”, “free”, “allocate”, “free”]
[[10], [1, 1], [1, 2], [1, 3], [2], [3, 4], [1, 1], [1, 1], [1], [10, 2], [7]]
输出
[null, 0, 1, 2, 1, 3, 1, 6, 3, -1, 0]

解释
Allocator loc = new Allocator(10); // 初始化一个大小为 10 的内存数组,所有内存单元都是空闲的。
loc.allocate(1, 1); // 最左侧的块的第一个下标是 0 。内存数组变为 [1, , , , , , , , , ]。返回 0 。
loc.allocate(1, 2); // 最左侧的块的第一个下标是 1 。内存数组变为 [1,2, , , , , , , , ]。返回 1 。
loc.allocate(1, 3); // 最左侧的块的第一个下标是 2 。内存数组变为 [1,2,3, , , , , , , ]。返回 2 。
loc.free(2); // 释放 mID 为 2 的所有内存单元。内存数组变为 [1, ,3, , , , , , , ] 。返回 1 ,因为只有 1 个 mID 为 2 的内存单元。
loc.allocate(3, 4); // 最左侧的块的第一个下标是 3 。内存数组变为 [1, ,3,4,4,4, , , , ]。返回 3 。
loc.allocate(1, 1); // 最左侧的块的第一个下标是 1 。内存数组变为 [1,1,3,4,4,4, , , , ]。返回 1 。
loc.allocate(1, 1); // 最左侧的块的第一个下标是 6 。内存数组变为 [1,1,3,4,4,4,1, , , ]。返回 6 。
loc.free(1); // 释放 mID 为 1 的所有内存单元。内存数组变为 [ , ,3,4,4,4, , , , ] 。返回 3 ,因为有 3 个 mID 为 1 的内存单元。
loc.allocate(10, 2); // 无法找出长度为 10 个连续空闲内存单元的空闲块,所有返回 -1 。
loc.free(7); // 释放 mID 为 7 的所有内存单元。内存数组保持原状,因为不存在 mID 为 7 的内存单元。返回 0 。

提示:

1 <= n, size, mID <= 1000
最多调用 allocate 和 free 方法 1000 次

解题思路

用 数组int[] memory表示内存空间,用字典结构midIndex表示内存标识为mid所占据的内存位置 Map<Integer, List> midIndex;直接采用暴力模拟即可

代码实现

class Allocator {
     int[] memory;

    Map<Integer, List<Integer>> midIndex;

    public Allocator(int n) {
        this.memory = new int[n];
        midIndex = new HashMap<>();
    }

    public int allocate(int size, int mID) {
        int currentMemorySize = 0;

        for (int i = 0; i < memory.length; i++) {
            if (currentMemorySize == size) {
                List<Integer> indexs = midIndex.getOrDefault(mID, new ArrayList<>());
                for (int j = i - size; j < i; j++) {
                    memory[j] = mID;
                    indexs.add(j);
                }
                midIndex.put(mID, indexs);
                return i - size;
            }
            if (memory[i] == 0) {
                currentMemorySize++;
            } else {
                currentMemorySize = 0;
            }
        }
        if (currentMemorySize == size) {
            List<Integer> indexs = midIndex.getOrDefault(mID, new ArrayList<>());
            for (int j = memory.length - size; j < memory.length; j++) {
                memory[j] = mID;
                indexs.add(j);
            }
            midIndex.put(mID, indexs);
            return memory.length - size;
        }
        return -1;
    }

    public int free(int mID) {
       List<Integer> indexs = midIndex.get(mID);
       if(indexs == null) {
           return 0;
       } else {
           for (Integer I: indexs) {
               memory[I] = 0;
           }
           midIndex.remove(mID);
           return indexs.size();
       }
    }
}

/**
 * Your Allocator object will be instantiated and called as such:
 * Allocator obj = new Allocator(n);
 * int param_1 = obj.allocate(size,mID);
 * int param_2 = obj.free(mID);
 */
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值