{"id":8459,"date":"2021-06-01T07:56:15","date_gmt":"2021-06-01T14:56:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8459"},"modified":"2021-06-01T07:57:03","modified_gmt":"2021-06-01T14:57:03","slug":"leetcode-1860-incremental-memory-leak","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1860-incremental-memory-leak\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1860. Incremental Memory Leak"},"content":{"rendered":"\n<p>You are given two integers&nbsp;<code>memory1<\/code>&nbsp;and&nbsp;<code>memory2<\/code>&nbsp;representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.<\/p>\n\n\n\n<p>At the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;second (starting from 1),&nbsp;<code>i<\/code>&nbsp;bits of memory are allocated to the stick with&nbsp;<strong>more available memory<\/strong>&nbsp;(or from the first memory stick if both have the same available memory). If neither stick has at least&nbsp;<code>i<\/code>&nbsp;bits of available memory, the program&nbsp;<strong>crashes<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array containing&nbsp;<\/em><code>[crashTime, memory1<sub>crash<\/sub>, memory2<sub>crash<\/sub>]<\/code><em>, where&nbsp;<\/em><code>crashTime<\/code><em>&nbsp;is the time (in seconds) when the program crashed and&nbsp;<\/em><code>memory1<sub>crash<\/sub><\/code><em>&nbsp;and&nbsp;<\/em><code>memory2<sub>crash<\/sub><\/code><em>&nbsp;are the available bits of memory in the first and second sticks respectively<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> memory1 = 2, memory2 = 2\n<strong>Output:<\/strong> [3,1,0]\n<strong>Explanation:<\/strong> The memory is allocated as follows:\n- At the 1<sup>st<\/sup> second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.\n- At the 2<sup>nd<\/sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.\n- At the 3<sup>rd<\/sup> second, the program crashes. The sticks have 1 and 0 bits available respectively.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> memory1 = 8, memory2 = 11\n<strong>Output:<\/strong> [6,0,4]\n<strong>Explanation:<\/strong> The memory is allocated as follows:\n- At the 1<sup>st<\/sup> second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.\n- At the 2<sup>nd<\/sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.\n- At the 3<sup>rd<\/sup> second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.\n- At the 4<sup>th<\/sup> second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.\n- At the 5<sup>th<\/sup> second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.\n- At the 6<sup>th<\/sup> second, the program crashes. The sticks have 0 and 4 bits available respectively.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= memory1, memory2 &lt;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(max(memory1, memory2)<sup>0.5<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def memLeak(self, memory1: int, memory2: int) -> List[int]:\n    for i in range(1, 2**30):\n      if max(memory1, memory2) < i:\n        return [i, memory1, memory2]\n      elif memory1 >= memory2:\n        memory1 -= i\n      else:\n        memory2 -= i\n    return None\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers&nbsp;memory1&nbsp;and&nbsp;memory2&nbsp;representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,179],"class_list":["post-8459","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8459"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8459\/revisions"}],"predecessor-version":[{"id":8461,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8459\/revisions\/8461"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}