g1801_1900.s1860_incremental_memory_leak.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1801_1900.s1860_incremental_memory_leak;
// #Medium #Simulation #2022_05_08_Time_5_ms_(78.57%)_Space_41.9_MB_(34.69%)
/**
* 1860 - Incremental Memory Leak\.
*
* Medium
*
* 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
**/
public class Solution {
public int[] memLeak(int memory1, int memory2) {
int time = 1;
while (memory1 >= time || memory2 >= time) {
if (memory1 >= memory2) {
memory1 -= time;
} else {
memory2 -= time;
}
time++;
}
return new int[] {time, memory1, memory2};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy