返回介绍

solution / 1800-1899 / 1860.Incremental Memory Leak / README_EN

发布于 2024-06-17 01:03:13 字数 5492 浏览 0 评论 0 收藏 0

1860. Incremental Memory Leak

中文文档

Description

You are given two integers memory1 and memory2 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.

At the ith second (starting from 1), i bits of memory are allocated to the stick with more available memory (or from the first memory stick if both have the same available memory). If neither stick has at least i bits of available memory, the program crashes.

Return _an array containing _[crashTime, memory1crash, memory2crash]_, where _crashTime_ is the time (in seconds) when the program crashed and _memory1crash_ and _memory2crash_ are the available bits of memory in the first and second sticks respectively_.

 

Example 1:

Input: memory1 = 2, memory2 = 2
Output: [3,1,0]
Explanation: The memory is allocated as follows:
- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.
- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively.

Example 2:

Input: memory1 = 8, memory2 = 11
Output: [6,0,4]
Explanation: The memory is allocated as follows:
- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.
- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.
- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.
- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.
- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively.

 

Constraints:

  • 0 <= memory1, memory2 <= 231 - 1

Solutions

Solution 1: Simulation

We directly simulate the allocation of memory.

Assume $t$ is the moment of unexpected exit, then the two memory sticks can definitely accommodate the memory consumed at the moment $t-1$ and before, so we have:

$$ \sum_{i=1}^{t-1} i = \frac{t\times (t-1)}{2} \leq (m_1+m_2) $$

The time complexity is $O(\sqrt{m_1+m_2})$, where $m_1$ and $m_2$ are the sizes of the two memory sticks respectively.

class Solution:
  def memLeak(self, memory1: int, memory2: int) -> List[int]:
    i = 1
    while i <= max(memory1, memory2):
      if memory1 >= memory2:
        memory1 -= i
      else:
        memory2 -= i
      i += 1
    return [i, memory1, memory2]
class Solution {
  public int[] memLeak(int memory1, int memory2) {
    int i = 1;
    for (; i <= Math.max(memory1, memory2); ++i) {
      if (memory1 >= memory2) {
        memory1 -= i;
      } else {
        memory2 -= i;
      }
    }
    return new int[] {i, memory1, memory2};
  }
}
class Solution {
public:
  vector<int> memLeak(int memory1, int memory2) {
    int i = 1;
    for (; i <= max(memory1, memory2); ++i) {
      if (memory1 >= memory2) {
        memory1 -= i;
      } else {
        memory2 -= i;
      }
    }
    return {i, memory1, memory2};
  }
};
func memLeak(memory1 int, memory2 int) []int {
  i := 1
  for ; i <= memory1 || i <= memory2; i++ {
    if memory1 >= memory2 {
      memory1 -= i
    } else {
      memory2 -= i
    }
  }
  return []int{i, memory1, memory2}
}
function memLeak(memory1: number, memory2: number): number[] {
  let i = 1;
  for (; i <= Math.max(memory1, memory2); ++i) {
    if (memory1 >= memory2) {
      memory1 -= i;
    } else {
      memory2 -= i;
    }
  }
  return [i, memory1, memory2];
}
/**
 * @param {number} memory1
 * @param {number} memory2
 * @return {number[]}
 */
var memLeak = function (memory1, memory2) {
  let i = 1;
  for (; i <= Math.max(memory1, memory2); ++i) {
    if (memory1 >= memory2) {
      memory1 -= i;
    } else {
      memory2 -= i;
    }
  }
  return [i, memory1, memory2];
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文