Title: #2502 - Design Memory Allocator - LeetCode JavaScript Solutions
Open Graph Title: #2502 - Design Memory Allocator - LeetCode JavaScript Solutions
X Title: #2502 - Design Memory Allocator - LeetCode JavaScript Solutions
Description: 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 allocat...
Open Graph Description: 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 allocat...
X Description: 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 allocat...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/design-memory-allocator
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/design-memory-allocator","name":"#2502 - Design Memory Allocator - LeetCode JavaScript Solutions","description":"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 allocat...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} n\n */\nvar Allocator = function(n) {\n this.memory = Array(n).fill(0);\n};\n\n/**\n * @param {number} size\n * @param {number} mID\n * @return {number}\n */\nAllocator.prototype.allocate = function(size, mID) {\n let start = -1;\n let count = 0;\n\n for (let i = 0; i < this.memory.length; i++) {\n if (this.memory[i] === 0) {\n if (start === -1) start = i;\n count++;\n if (count === size) {\n for (let j = start; j < start + size; j++) {\n this.memory[j] = mID;\n }\n return start;\n }\n } else {\n start = -1;\n count = 0;\n }\n }\n\n return -1;\n};\n\n/**\n * @param {number} mID\n * @return {number}\n */\nAllocator.prototype.freeMemory = function(mID) {\n let result = 0;\n\n for (let i = 0; i < this.memory.length; i++) {\n if (this.memory[i] === mID) {\n this.memory[i] = 0;\n result++;\n }\n }\n\n return result;\n};","keywords":"LeetCode 2502, #2502 - Design Memory Allocator, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/design-memory-allocator |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/design-memory-allocator/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/2502-design-memory-allocator.js |
| Array | https://leetcodejavascript.com/tags/array |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Simulation | https://leetcodejavascript.com/tags/simulation |
| Design | https://leetcodejavascript.com/tags/design |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow