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
This problem simulates memory allocation to two memory sticks until a crash occurs. The core idea is to iteratively allocate increasing amounts of memory (1, 2, 3, ... bits) to the stick with more available memory at each step. The simulation stops when neither stick has enough memory for the current allocation.
Algorithm:
Initialization: Start with the given memory1
and memory2
representing the initial memory in each stick. Initialize a time counter i
to 1.
Iteration: Repeat the following steps until a crash occurs:
memory1
and memory2
.i
bits of memory to the stick with more memory (or memory1
if they are equal). Update the memory of that stick by subtracting i
.i
(the time counter).Crash Condition: The simulation crashes when both memory1
and memory2
are less than i
. This is because there isn't enough memory to allocate i
bits.
Return: Return an array [i, memory1, memory2]
, where i
is the time of the crash, and memory1
and memory2
are the remaining memory in each stick.
Time Complexity Analysis:
The simulation continues until the sum of memory in both sticks is less than the sum of integers from 1 to i-1
. The sum of integers from 1 to n
is given by n(n+1)/2
. Therefore, the loop will continue approximately until:
i(i-1)/2 <= memory1 + memory2
Solving for i
gives us:
i ≈ √(2 * (memory1 + memory2))
This means the number of iterations is proportional to the square root of the total initial memory. Therefore, the time complexity of this approach is O(√(memory1 + memory2)). This is because the loop iterates approximately √(memory1 + memory2) times.
Space Complexity Analysis:
The space complexity is O(1) because we only use a constant number of variables to store the memory in each stick and the time counter.
Code Examples (with explanations):
The provided code in multiple languages implements the algorithm directly. Here's a breakdown of the Python code, as an example:
class Solution:
def memLeak(self, memory1: int, memory2: int) -> List[int]:
i = 1 # Initialize time counter
while i <= max(memory1, memory2): # loop until crash
if memory1 >= memory2:
memory1 -= i # Allocate to memory1
else:
memory2 -= i # Allocate to memory2
i += 1 # Increment time
return [i, memory1, memory2] #return crash time and remaining memory
The other languages (Java, C++, Go, TypeScript, JavaScript) follow the same logic, with minor syntactic differences. They all directly simulate the memory allocation process until the crash condition is met.