leetcode 2502. 设计内存分配器 中等

embedded/2025/2/26 16:49:15/

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

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

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

注意:

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

实现 Allocator 类:

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

示例:

输入
["Allocator", "allocate", "allocate", "allocate", "freeMemory", "allocate", "allocate", "allocate", "freeMemory", "allocate", "freeMemory"]
[[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.freeMemory(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.freeMemory(1); // 释放 mID 为 1 的所有内存单元。内存数组变为 [ , ,3,4,4,4, , , , ] 。返回 3 ,因为有 3 个 mID 为 1 的内存单元。
loc.allocate(10, 2); // 无法找出长度为 10 个连续空闲内存单元的空闲块,所有返回 -1 。
loc.freeMemory(7); // 释放 mID 为 7 的所有内存单元。内存数组保持原状,因为不存在 mID 为 7 的内存单元。返回 0 。

提示:

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

分析:直接模拟即可。实现int allocate(int size, int mID)函数时,每次从最初的位置开始查询是否空闲,并记录连续空闲空间的个数。如果某个位置已经分配,则将当前的连续空间计数重置为0.当连续空闲空间计数为size时,说明可以分配,起点位于当前位置的前size处。

typedef struct {int n;int *memory;
} Allocator;Allocator* allocatorCreate(int n) {Allocator *p=(Allocator*)malloc(sizeof(Allocator));p->n=n;p->memory=(int*)malloc(sizeof(int)*n);for(int i=0;i<n;++i)p->memory[i]=0;return p;
}int allocatorAllocate(Allocator* obj, int size, int mID) {int n=obj->n,cnt=0;for(int i=0;i<n;++i){if(obj->memory[i]==0)cnt++;else cnt=0;if(cnt==size){for(int j=i-cnt+1;j<=i;++j)obj->memory[j]=mID;return i-cnt+1;}}return -1;
}int allocatorFreeMemory(Allocator* obj, int mID) {int n=obj->n,cnt=0;for(int i=0;i<n;++i)if(obj->memory[i]==mID)cnt++,obj->memory[i]=0;return cnt;
}void allocatorFree(Allocator* obj) {free(obj->memory);free(obj);return;
}/*** Your Allocator struct will be instantiated and called as such:* Allocator* obj = allocatorCreate(n);* int param_1 = allocatorAllocate(obj, size, mID);* int param_2 = allocatorFreeMemory(obj, mID);* allocatorFree(obj);
*/


http://www.ppmy.cn/embedded/167305.html

相关文章

10. docker nginx官方镜像使用方法

本文介绍docker nginx官方镜像使用方法&#xff0c;因为第一次用&#xff0c;在加上对docker也不是很熟&#xff0c;中间踩了一些坑&#xff0c;为了避免下一次用又踩坑&#xff0c;因此记录如下&#xff0c;也希望能够帮到其它小伙伴。 官方镜像页面&#xff1a;https://hub.d…

qt中QDebuge中文乱码的解决

qt的QDebuge中文乱码&#xff0c;我采用的下面的方案&#xff0c;直接在Windows的设置中修改&#xff0c;然后就OK了&#xff0c;记录一下。可能不同的开发环境不同吧&#xff0c;我用的是win11&#xff0c;按照下图设置&#xff0c;然后重启就OK了。

【uniapp-Vue3】beforeRegister在注册用户入库前设置初始用户

关于uniCloud的beforeRegister钩子的具体介绍和用法在下面&#xff1a; uniCloudhttps://doc.dcloud.net.cn/uniCloud/uni-id/summary.html#before-register首先在uniCloud/cloudfunctions/common/uni-config-center/uni-id中创建hooks文件&#xff0c;再创建index.js文件 在…

滴水逆向_引用_友元函数_运算符重载

作业&#xff1a; 运算符号重载实现。 struct Person { public:int x;int y; public:Person(){this->x 10;this->y 20;}Person(int x, int y){this->x x;this->y y;}//申明友元函数void Printf(const Person& p){printf("%d %d",p.x,p.y);}/…

工具方法 - 合规性矩阵

Compliance matrix &#xff08;合规性矩阵&#xff09;是产品需求管理中的一个重要工具&#xff0c;它是用来识别、跟踪、监控和组织所有客户和利益相关方需求是否被满足的工具。具体来说&#xff0c;Compliance matrix需要用一行一行的证据来证明被设计的产品针对每个需求的实…

std::thread的同步机制

在 C 中&#xff0c;std::thread 用于创建和管理线程。为了确保多个线程能正确、安全地访问共享资源&#xff0c;避免数据竞争和不一致问题&#xff0c;需要使用同步机制。 互斥锁&#xff08;std::mutex&#xff09; 原理&#xff1a;互斥锁是一种最基本的同步原语&#xff…

跟着李沐老师学习深度学习(十六)

继续学习深度学习&#xff08;十六&#xff09; 继续理解transformer 对于transformer的理解感觉还是云里雾里的&#xff0c;今天又找了一些视频进行一个梳理。 一个浅解 在B站学习发现评论区真的很不错&#xff0c;在沐神讲transformer论文的评论下&#xff0c;有一个评论…

AR技术下的电商:虚拟试穿/试用/试戴成新风尚

随着科技的日新月异&#xff0c;增强现实&#xff08;AR&#xff09;技术正悄然改变着我们的生活&#xff0c;尤其在电子商务领域&#xff0c;AR技术的融入正掀起一场前所未有的变革。那么&#xff0c;AR技术究竟是何方神圣&#xff1f;它在电商领域又展现出了哪些非凡的应用呢…