Mid/SeniorMathSimulation

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 i^th second, starting from 1, i bits of memory are allocated to the stick with more available memory, or to the first memory stick if both sticks 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
Inputmemory1 = 2, memory2 = 2
Output[3,1,0]
After allocating 1 bit to stick 1 and 2 bits to stick 2, the program crashes at the 3rd second with 1 and 0 bits remaining.
Example 2
Inputmemory1 = 8, memory2 = 11
Output[6,0,4]
The allocations leave the sticks with 0 and 4 bits available, so the program crashes at the 6th second.

Constraints

  • 0 <= memory1, memory2 <= 2^31 - 1

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate