|
| 1 | +// Source : https://leetcode.com/problems/incremental-memory-leak/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-05-22 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given two integers memory1 and memory2 representing the available memory in bits on two |
| 8 | + * memory sticks. There is currently a faulty program running that consumes an increasing amount of |
| 9 | + * memory every second. |
| 10 | + * |
| 11 | + * At the i^th second (starting from 1), i bits of memory are allocated to the stick with more |
| 12 | + * available memory (or from the first memory stick if both have the same available memory). If |
| 13 | + * neither stick has at least i bits of available memory, the program crashes. |
| 14 | + * |
| 15 | + * Return an array containing [crashTime, memory1crash, memory2crash], where crashTime is the time (in |
| 16 | + * seconds) when the program crashed and memory1crash and memory2crash are the available bits of |
| 17 | + * memory in the first and second sticks respectively. |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * |
| 21 | + * Input: memory1 = 2, memory2 = 2 |
| 22 | + * Output: [3,1,0] |
| 23 | + * Explanation: The memory is allocated as follows: |
| 24 | + * - At the 1^st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of |
| 25 | + * available memory. |
| 26 | + * - At the 2^nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of |
| 27 | + * available memory. |
| 28 | + * - At the 3^rd second, the program crashes. The sticks have 1 and 0 bits available respectively. |
| 29 | + * |
| 30 | + * Example 2: |
| 31 | + * |
| 32 | + * Input: memory1 = 8, memory2 = 11 |
| 33 | + * Output: [6,0,4] |
| 34 | + * Explanation: The memory is allocated as follows: |
| 35 | + * - At the 1^st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of |
| 36 | + * available memory. |
| 37 | + * - At the 2^nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of |
| 38 | + * available memory. |
| 39 | + * - At the 3^rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of |
| 40 | + * available memory. |
| 41 | + * - At the 4^th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of |
| 42 | + * available memory. |
| 43 | + * - At the 5^th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of |
| 44 | + * available memory. |
| 45 | + * - At the 6^th second, the program crashes. The sticks have 0 and 4 bits available respectively. |
| 46 | + * |
| 47 | + * Constraints: |
| 48 | + * |
| 49 | + * 0 <= memory1, memory2 <= 2^31 - 1 |
| 50 | + ******************************************************************************************************/ |
| 51 | + |
| 52 | +class Solution { |
| 53 | +public: |
| 54 | + vector<int> memLeak(int memory1, int memory2) { |
| 55 | + int i=0; |
| 56 | + while(++i) { |
| 57 | + int& himem = memory1 >= memory2 ? memory1 : memory2; |
| 58 | + if (himem < i) break; |
| 59 | + himem -= i; |
| 60 | + } |
| 61 | + vector<int> result={i, memory1, memory2}; |
| 62 | + return result; |
| 63 | + } |
| 64 | +}; |
0 commit comments