Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1860
Browse files Browse the repository at this point in the history
No.1860.Incremental Memory Leak
  • Loading branch information
yanglbme committed Sep 7, 2022
1 parent 9871e52 commit 7323938
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 21 deletions.
55 changes: 48 additions & 7 deletions solution/1800-1899/1860.Incremental Memory Leak/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:模拟**

直接模拟内存的分配。

假设 $t$ 为意外退出的时刻,那么两个内存条一定可以容纳 $t-1$ 时刻及以前消耗的内存,因此有:

$$
\sum_{i=1}^{t-1} i = \frac{t\times (t-1)}{2} \leq (m_1+m_2)
$$

时间复杂度 $O(\sqrt{m1+m2})$,其中 $m_1$, $m_2$ 分别为两个内存条的内存大小。

<!-- tabs:start -->

### **Python3**
Expand All @@ -59,7 +71,7 @@
class Solution:
def memLeak(self, memory1: int, memory2: int) -> List[int]:
i = 1
while memory1 >= i or memory2 >= i:
while i <= max(memory1, memory2):
if memory1 >= memory2:
memory1 -= i
else:
Expand All @@ -76,13 +88,12 @@ class Solution:
class Solution {
public int[] memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return new int[] {i, memory1, memory2};
}
Expand All @@ -99,13 +110,12 @@ class Solution {
*/
var memLeak = function (memory1, memory2) {
let i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
i++;
}
return [i, memory1, memory2];
};
Expand All @@ -118,19 +128,50 @@ class Solution {
public:
vector<int> memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return {i, memory1, memory2};
}
};
```
### **Go**
```go
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}
}
```

### **TypeScript**

```ts
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];
}
```

### **...**

```
Expand Down
43 changes: 36 additions & 7 deletions solution/1800-1899/1860.Incremental Memory Leak/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
class Solution:
def memLeak(self, memory1: int, memory2: int) -> List[int]:
i = 1
while memory1 >= i or memory2 >= i:
while i <= max(memory1, memory2):
if memory1 >= memory2:
memory1 -= i
else:
Expand All @@ -68,13 +68,12 @@ class Solution:
class Solution {
public int[] memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return new int[] {i, memory1, memory2};
}
Expand All @@ -91,13 +90,12 @@ class Solution {
*/
var memLeak = function (memory1, memory2) {
let i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
i++;
}
return [i, memory1, memory2];
};
Expand All @@ -110,19 +108,50 @@ class Solution {
public:
vector<int> memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return {i, memory1, memory2};
}
};
```
### **Go**
```go
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}
}
```

### **TypeScript**

```ts
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];
}
```

### **...**

```
Expand Down
3 changes: 1 addition & 2 deletions solution/1800-1899/1860.Incremental Memory Leak/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ class Solution {
public:
vector<int> memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return {i, memory1, memory2};
}
Expand Down
11 changes: 11 additions & 0 deletions solution/1800-1899/1860.Incremental Memory Leak/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class Solution {
public int[] memLeak(int memory1, int memory2) {
int i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
++i;
}
return new int[] {i, memory1, memory2};
}
Expand Down
3 changes: 1 addition & 2 deletions solution/1800-1899/1860.Incremental Memory Leak/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
*/
var memLeak = function (memory1, memory2) {
let i = 1;
while (memory1 >= i || memory2 >= i) {
for (; i <= Math.max(memory1, memory2); ++i) {
if (memory1 >= memory2) {
memory1 -= i;
} else {
memory2 -= i;
}
i++;
}
return [i, memory1, memory2];
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def memLeak(self, memory1: int, memory2: int) -> List[int]:
i = 1
while memory1 >= i or memory2 >= i:
while i <= max(memory1, memory2):
if memory1 >= memory2:
memory1 -= i
else:
Expand Down
11 changes: 11 additions & 0 deletions solution/1800-1899/1860.Incremental Memory Leak/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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];
}

0 comments on commit 7323938

Please sign in to comment.