Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1860.html
1860. Incremental Memory Leak
Level
Medium
Description
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 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, memory1_crash, memory2_crash]
, where crashTime
is the time (in seconds) when the program crashed and memory1_crash
and memory2_crash
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 <= 2^31 - 1
Solution
Simulate the process. Maintain a variable time
with initial value 0. Each time increase time
by 1 and update either memory1
or memory2
. Once both memory1
and memory2
are less than time
, the program crashes, so return the array [time, memory1, memory2]
.
-
class Solution { public int[] memLeak(int memory1, int memory2) { int time = 0; boolean flag = true; while (flag) { time++; if (memory1 < time && memory2 < time) flag = false; else { if (memory1 >= memory2) memory1 -= time; else memory2 -= time; } } return new int[]{time, memory1, memory2}; } } ############ class Solution { public int[] memLeak(int memory1, int memory2) { int i = 1; for (; i <= Math.max(memory1, memory2); ++i) { if (memory1 >= memory2) { memory1 -= i; } else { memory2 -= i; } } return new int[] {i, memory1, memory2}; } }
-
// OJ: https://leetcode.com/problems/incremental-memory-leak/ // Time: O(sqrt(A + B)) // Space: O(1) class Solution { public: vector<int> memLeak(int A, int B) { int i = 1; while (A >= i || B >= i) { if (A >= B) { A -= i; } else { B -= i; } ++i; } return {i, A, B}; } };
-
class Solution: def memLeak(self, memory1: int, memory2: int) -> List[int]: i = 1 while i <= max(memory1, memory2): if memory1 >= memory2: memory1 -= i else: memory2 -= i i += 1 return [i, memory1, memory2] ############ # 1860. Incremental Memory Leak # https://leetcode.com/problems/incremental-memory-leak/ class Solution: def memLeak(self, m1: int, m2: int) -> List[int]: t = 1 while m1 > 0 or m2 > 0: if m1 >= m2: if m1 >= t: m1 -= t else: break else: if m2 >= t: m2 -= t else: break t += 1 return [t, m1, m2]
-
func memLeak(memory1 int, memory2 int) []int { i := 1 for ; i <= memory1 || i <= memory2; i++ { if memory1 >= memory2 { memory1 -= i } else { memory2 -= i } } return []int{i, memory1, memory2} }
-
function memLeak(memory1: number, memory2: number): number[] { let i = 1; for (; i <= Math.max(memory1, memory2); ++i) { if (memory1 >= memory2) { memory1 -= i; } else { memory2 -= i; } } return [i, memory1, memory2]; }
-
/** * @param {number} memory1 * @param {number} memory2 * @return {number[]} */ var memLeak = function (memory1, memory2) { let i = 1; for (; i <= Math.max(memory1, memory2); ++i) { if (memory1 >= memory2) { memory1 -= i; } else { memory2 -= i; } } return [i, memory1, memory2]; };