Skip to content

Commit 406e77e

Browse files
committed
New Problem Solution - "1860. Incremental Memory Leak"
1 parent f0dd110 commit 406e77e

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1860|[Incremental Memory Leak](https://leetcode.com/problems/incremental-memory-leak/) | [C++](./algorithms/cpp/incrementalMemoryLeak/IncrementalMemoryLeak.cpp)|Medium|
1213
|1859|[Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/) | [C++](./algorithms/cpp/sortingTheSentence/SortingTheSentence.cpp)|Easy|
1314
|1857|[Largest Color Value in a Directed Graph](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/) | [C++](./algorithms/cpp/largestColorValueInADirectedGraph/LargestColorValueInADirectedGraph.cpp)|Hard|
1415
|1856|[Maximum Subarray Min-Product](https://leetcode.com/problems/maximum-subarray-min-product/) | [C++](./algorithms/cpp/maximumSubarrayMinProduct/MaximumSubarrayMinProduct.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)