当前位置: 首页 > wzjs >正文

福建省建设职业注册资格管理中心网站网页制作教程软件

福建省建设职业注册资格管理中心网站,网页制作教程软件,盘锦网站制作,wordpress学校主题【LetMeFly】2502.设计内存分配器:暴力模拟 力扣题目链接:https://leetcode.cn/problems/design-memory-allocator/ 给你一个整数 n ,表示下标从 0 开始的内存数组的大小。所有内存单元开始都是空闲的。 请你设计一个具备以下功能的内存分…

【LetMeFly】2502.设计内存分配器:暴力模拟

力扣题目链接:https://leetcode.cn/problems/design-memory-allocator/

给你一个整数 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
  • 最多调用 allocatefree 方法 1000

解题方法:暴力模拟

看题过程中一直在想怎么设计,结果一看数据量只有1000,所以决定暴力模拟了。直接使用大小为 n n n的数据模拟内存数组。

  • 初始化:每个内存单元都为 0 0 0
  • 分配:按下标从小到大遍历内存单元,一旦出现连续 s i z e size size个为 0 0 0的内存单元,就将这些内存单元分配给 m I D mID mID;若分配失败则返回 − 1 -1 1
  • 释放:遍历内存单元,统计值为 m I D mID mID的内存单元的个数并将它们标记为 0 0 0

以上。

  • 时间复杂度:初始化 O ( n ) O(n) O(n),单次分配 O ( n ) O(n) O(n),单次释放 O ( n ) O(n) O(n)
  • 空间复杂度 O ( n ) O(n) O(n)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-02-25 16:18:52* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-02-25 16:30:51*/
class Allocator {
private:vector<int> v;int n;
public:Allocator(int n): n(n), v(n) {}int allocate(int size, int mID) {for (int l = -1, r = 0, cnt = 0; r < n; r++) {if (v[r]) {cnt = 0;l = r;continue;}cnt++;if (cnt == size) {while (++l <= r) {v[l] = mID;}return r - size + 1;}}return -1;}int freeMemory(int mID) {int ans = 0;for (int &t : v) {if (t == mID) {ans++;t = 0;}}return ans;}
};/*** 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->freeMemory(mID);*/
Python
'''
Author: LetMeFly
Date: 2025-02-25 16:19:05
LastEditors: LetMeFly.xyz
LastEditTime: 2025-02-25 16:34:50
'''
class Allocator:def __init__(self, n: int):self.v = [None] * ndef allocate(self, size: int, mID: int) -> int:l, cnt = -1, 0for r in range(len(self.v)):if self.v[r]:l, cnt = r, 0continuecnt += 1if cnt == size:for i in range(l + 1, r + 1):self.v[i] = mIDreturn l + 1return -1def freeMemory(self, mID: int) -> int:ans = 0for i in range(len(self.v)):if self.v[i] == mID:self.v[i] = 0ans += 1return ans# Your Allocator object will be instantiated and called as such:
# obj = Allocator(n)
# param_1 = obj.allocate(size,mID)
# param_2 = obj.freeMemory(mID)
Java
/** @Author: LetMeFly* @Date: 2025-02-25 16:19:08* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-02-25 16:38:15*/
class Allocator {private int[] v;private int n;public Allocator(int n) {v = new int[n];this.n = n;}public int allocate(int size, int mID) {for (int l = -1, r = 0, cnt = 0; r < n; r++) {if (v[r] != 0) {cnt = 0;l = r;continue;}cnt++;if (cnt == size) {while (++l <= r) {v[l] = mID;}return r - size + 1;}}return -1;}public int freeMemory(int mID) {int ans = 0;for (int i = 0; i < n; i++) {if (v[i] == mID) {ans++;v[i] = 0;}}return ans;}
}/*** 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.freeMemory(mID);*/
Go
/** @Author: LetMeFly* @Date: 2025-02-25 16:19:11* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-02-25 16:45:09*/
package maintype Allocator struct {v []int
}func Constructor(n int) Allocator {return Allocator{v: make([]int, n),}
}func (this *Allocator) Allocate(size int, mID int) int {for r, cnt := 0, 0; r < len(this.v); r++ {if this.v[r] != 0 {cnt = 0continue}cnt++if (cnt == size) {for ; size > 0; size, r = size - 1, r - 1 {this.v[r] = mID}return r + 1}}return -1
}func (this *Allocator) FreeMemory(mID int) (ans int) {for i, v := range this.v {if v == mID {ans++this.v[i] = 0}}return
}/*** Your Allocator object will be instantiated and called as such:* obj := Constructor(n);* param_1 := obj.Allocate(size,mID);* param_2 := obj.FreeMemory(mID);*/

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源


文章转载自:

http://HuWHL9wy.fkgqn.cn
http://fCRgGkZe.fkgqn.cn
http://nMj03bob.fkgqn.cn
http://Du8tZt1h.fkgqn.cn
http://XoXlDiiD.fkgqn.cn
http://2Yv5KDa3.fkgqn.cn
http://On0OTv2e.fkgqn.cn
http://PSDRKMkU.fkgqn.cn
http://C7fjVXtG.fkgqn.cn
http://sWwwpfHB.fkgqn.cn
http://5phgmuy2.fkgqn.cn
http://EESpuM9h.fkgqn.cn
http://zDTCT3k7.fkgqn.cn
http://3nvakw4s.fkgqn.cn
http://UJEZBkEK.fkgqn.cn
http://4y5tIMCD.fkgqn.cn
http://WiYjKJqT.fkgqn.cn
http://KGOZDxfI.fkgqn.cn
http://lgea2Pav.fkgqn.cn
http://08JGMThj.fkgqn.cn
http://l4oJiSf4.fkgqn.cn
http://nKjPk2zS.fkgqn.cn
http://gV6UIOgi.fkgqn.cn
http://XIfsq0Nt.fkgqn.cn
http://la4RAiLZ.fkgqn.cn
http://9RPpvJLq.fkgqn.cn
http://ICHkMoFz.fkgqn.cn
http://FQooobmb.fkgqn.cn
http://ZOMa8rqU.fkgqn.cn
http://6QryP84r.fkgqn.cn
http://www.dtcms.com/wzjs/633262.html

相关文章:

  • 做网站建设要学多久东莞市镇街建设项目监理招标网站
  • 织梦的网站关键词站长工具综合查询2020
  • wordpress只显示首页网站seo的关键词排名怎么做的
  • 网站设计分析报告做手机网站和pc如何做
  • 网站跳出率 查询湖人排名最新
  • 重庆专业的网站建设慈溪外贸公司网站
  • 做违规网站景区微网站 建设方案
  • 网页设计是网站建设与管理的内容吗做游戏直播那个网站好
  • 思帽西宁网站建设湖北响应式网站制作
  • 公益网站模板西安谷歌推广
  • 网站不会更新文章深圳建设网站和公众号
  • 网站建设教程纯正苏州久远网络房地产网站开发公司
  • 网站服务器用什么好处网站静态和动态区别
  • 梵克雅宝五花手链徐州seo推广公司
  • 网站制作旅行社logo设计在线生成免费图片加文字
  • 关于教育网站的策划书青海网站建设 小程序开发
  • 网站后台管理系统展望常用网站后缀
  • 外贸网站怎么做seo网站开发介绍费
  • 石家庄网站建设布局软文推广代理
  • 怎样做境外网站seo网络推广培训班
  • 青岛网站建设维护企业网站建设的作用
  • 信用徐州网站建设情况网站技术支持什么意思
  • wordpress站点统计小工具做食品怎样选网站
  • 专门做护理PDCA的网站网站开发软件有
  • wap网站开发框架wordpress4.7.4漏洞
  • 百度网站 收录企业年报查询
  • 深圳网站设计南京火车头wordpress发布模块4.9
  • 站长推广网网站模板有哪些内容
  • 设计最简单的企业网站珠海网站建设科速互联
  • 衡水网站建设公司联系电话陕西专业网站建设公司