1860. Incremental Memory Leak LeetCode Solution

In this guide, you will get 1860. Incremental Memory Leak LeetCode Solution with the best time and space complexity. The solution to Incremental Memory Leak problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Incremental Memory Leak solution in C++
  4. Incremental Memory Leak solution in Java
  5. Incremental Memory Leak solution in Python
  6. Additional Resources
1860. Incremental Memory Leak LeetCode Solution image

Problem Statement of Incremental Memory Leak

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.

See also  875. Koko Eating Bananas LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(\log(x + y)), where x = memory1 and y = memory2
  • Space Complexity: O(1)

1860. Incremental Memory Leak LeetCode Solution in C++

class Solution {
 public:
  vector<int> memLeak(int memory1, int memory2) {
    int i = 1;

    while (memory1 >= i || memory2 >= i) {
      if (memory1 >= memory2)
        memory1 -= i;
      else
        memory2 -= i;
      ++i;
    }

    return {i, memory1, memory2};
  }
};
/* code provided by PROGIEZ */

1860. Incremental Memory Leak LeetCode Solution in Java

class Solution {
  public int[] memLeak(int memory1, int memory2) {
    int i = 1;

    while (memory1 >= i || memory2 >= i) {
      if (memory1 >= memory2)
        memory1 -= i;
      else
        memory2 -= i;
      ++i;
    }

    return new int[] {i, memory1, memory2};
  }
}
// code provided by PROGIEZ

1860. Incremental Memory Leak LeetCode Solution in Python

class Solution:
  def memLeak(self, memory1: int, memory2: int) -> list[int]:
    i = 1

    while memory1 >= i or memory2 >= i:
      if memory1 >= memory2:
        memory1 -= i
      else:
        memory2 -= i
      i += 1

    return [i, memory1, memory2]
# code by PROGIEZ

Additional Resources

See also  2965. Find Missing and Repeated Values LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.