From 732393814c2e2ea4b47cfdf02d897f2e5fdb124b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 7 Sep 2022 17:17:55 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1860 No.1860.Incremental Memory Leak --- .../1860.Incremental Memory Leak/README.md | 55 ++++++++++++++++--- .../1860.Incremental Memory Leak/README_EN.md | 43 ++++++++++++--- .../1860.Incremental Memory Leak/Solution.cpp | 3 +- .../1860.Incremental Memory Leak/Solution.go | 11 ++++ .../Solution.java | 3 +- .../1860.Incremental Memory Leak/Solution.js | 3 +- .../1860.Incremental Memory Leak/Solution.py | 2 +- .../1860.Incremental Memory Leak/Solution.ts | 11 ++++ 8 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 solution/1800-1899/1860.Incremental Memory Leak/Solution.go create mode 100644 solution/1800-1899/1860.Incremental Memory Leak/Solution.ts diff --git a/solution/1800-1899/1860.Incremental Memory Leak/README.md b/solution/1800-1899/1860.Incremental Memory Leak/README.md index ba57d9a2f8b84..a52b5a27554ba 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/README.md +++ b/solution/1800-1899/1860.Incremental Memory Leak/README.md @@ -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$ 分别为两个内存条的内存大小。 + ### **Python3** @@ -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: @@ -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}; } @@ -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]; }; @@ -118,19 +128,50 @@ class Solution { public: vector 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]; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1860.Incremental Memory Leak/README_EN.md b/solution/1800-1899/1860.Incremental Memory Leak/README_EN.md index 757bf383bd134..73e800ad3eb03 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/README_EN.md +++ b/solution/1800-1899/1860.Incremental Memory Leak/README_EN.md @@ -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: @@ -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}; } @@ -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]; }; @@ -110,19 +108,50 @@ class Solution { public: vector 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]; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.cpp b/solution/1800-1899/1860.Incremental Memory Leak/Solution.cpp index eab94b53ede00..6b5b66453de2a 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/Solution.cpp +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.cpp @@ -2,13 +2,12 @@ class Solution { public: vector 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}; } diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.go b/solution/1800-1899/1860.Incremental Memory Leak/Solution.go new file mode 100644 index 0000000000000..0a344c31e40c5 --- /dev/null +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.go @@ -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} +} \ No newline at end of file diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.java b/solution/1800-1899/1860.Incremental Memory Leak/Solution.java index 0a9788f757e1d..822336753c156 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/Solution.java +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.java @@ -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}; } diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.js b/solution/1800-1899/1860.Incremental Memory Leak/Solution.js index 9ba6897d0aab4..40f55ebe962c8 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/Solution.js +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.js @@ -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]; }; diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.py b/solution/1800-1899/1860.Incremental Memory Leak/Solution.py index d36bbc66e4f1d..7b90a8ed8f865 100644 --- a/solution/1800-1899/1860.Incremental Memory Leak/Solution.py +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.py @@ -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: diff --git a/solution/1800-1899/1860.Incremental Memory Leak/Solution.ts b/solution/1800-1899/1860.Incremental Memory Leak/Solution.ts new file mode 100644 index 0000000000000..ea48d59b2ffb5 --- /dev/null +++ b/solution/1800-1899/1860.Incremental Memory Leak/Solution.ts @@ -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]; +}