Skip to content

1860. Incremental Memory Leak

  • Time: $O(\log(x + y))$, where x = memory1 and y = memory2
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  vector<int> memLeak(int memory1, int memory2) {
    int i = 1;

    while (memory1 >= i || memory2 >= i) {
      if (memory1 >= memory2)
        memory1 -= i;
      else
        memory2 -= i;
      ++i;
    }

    return {i, memory1, memory2};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  public int[] memLeak(int memory1, int memory2) {
    int i = 1;

    while (memory1 >= i || memory2 >= i) {
      if (memory1 >= memory2)
        memory1 -= i;
      else
        memory2 -= i;
      ++i;
    }

    return new int[] {i, memory1, memory2};
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def memLeak(self, memory1: int, memory2: int) -> list[int]:
    i = 1

    while memory1 >= i or memory2 >= i:
      if memory1 >= memory2:
        memory1 -= i
      else:
        memory2 -= i
      i += 1

    return [i, memory1, memory2]