[LeetCode] Incremental Memory Leak

1860. 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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<int> memLeak(int A, int B) {
int time = 1;
while(A >= time or B >= time) {
int& ma = A >= B ? A : B;
ma -= time++;
}
return {time, A, B};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/22/PS/LeetCode/incremental-memory-leak/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.