Skip to main content
Back to problems
#1860
Medium Algorithms

Incremental memory leak

Math Simulation
73.0% acceptance
Feb 25, 2026
235
93
You are given two integers memory1 and memory2 representing the available memory in bits on two memory sticks. 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].

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn mem_leak(mut memory1: i32, mut memory2: i32) -> Vec<i32> {
    let mut i = 1i32;
    loop {
      if memory1 >= memory2 {
        if memory1 < i { break; }
        memory1 -= i;
      } else {
        if memory2 < i { break; }
        memory2 -= i;
      }
      i += 1;
    }
    vec![i, memory1, memory2]
  }
}