diff --git a/leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md b/leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md index 23e6e0294..9ae2bf43e 100755 --- a/leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md +++ b/leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md @@ -1,28 +1,36 @@ # [1860.Incremental Memory Leak][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two integers `memory1` and `mmeory2` representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second. + +At the `ith` second (starting from 1), `i` bits of memory are allocated to the stick with **more available memory** (or from the first memory stick if both have the same available memory). If neither stick has at least `i` bits of available memory, the program **crashes**. + +Return an array containing `[crashTime, memory1crash, memory2crash]`, where crashTime is the time (in seconds) when the program crashed and `memory1crash` and `memory2crash` are the available bits of memory in the first and second sticks respectively. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: memory1 = 2, memory2 = 2 +Output: [3,1,0] +Explanation: The memory is allocated as follows: +- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory. +- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory. +- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Incremental Memory Leak -```go ``` - +Input: memory1 = 8, memory2 = 11 +Output: [6,0,4] +Explanation: The memory is allocated as follows: +- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory. +- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory. +- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory. +- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory. +- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory. +- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively. +``` ## 结语 diff --git a/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution.go b/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution.go index d115ccf5e..643d2127b 100644 --- a/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution.go +++ b/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution.go @@ -1,5 +1,14 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(memory1 int, memory2 int) []int { + ans := []int{1, memory1, memory2} + for ans[0] <= ans[1] || ans[0] <= ans[2] { + if ans[1] >= ans[2] { + ans[1] -= ans[0] + } else { + ans[2] -= ans[0] + } + ans[0]++ + } + return ans } diff --git a/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go b/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go index 14ff50eb4..96d9b6d98 100644 --- a/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go +++ b/leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + memory1, memory2 int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 2, 2, []int{3, 1, 0}}, + {"TestCase2", 8, 11, []int{6, 0, 4}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.memory1, c.memory2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.memory1, c.memory2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }