Skip to content

Commit 28caba2

Browse files
authored
Merge pull request #1255 from 0xff-dev/1860
Add solution and test-cases for problem 1860
2 parents 322ffde + d6c290e commit 28caba2

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1860.Incremental Memory Leak][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
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**.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: memory1 = 2, memory2 = 2
14+
Output: [3,1,0]
15+
Explanation: The memory is allocated as follows:
16+
- At the 1st second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.
17+
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.
18+
- At the 3rd second, the program crashes. The sticks have 1 and 0 bits available respectively.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Incremental Memory Leak
23-
```go
2423
```
25-
24+
Input: memory1 = 8, memory2 = 11
25+
Output: [6,0,4]
26+
Explanation: The memory is allocated as follows:
27+
- At the 1st second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.
28+
- At the 2nd second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.
29+
- At the 3rd second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.
30+
- At the 4th second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.
31+
- At the 5th second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.
32+
- At the 6th second, the program crashes. The sticks have 0 and 4 bits available respectively.
33+
```
2634

2735
## 结语
2836

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(memory1 int, memory2 int) []int {
4+
ans := []int{1, memory1, memory2}
5+
for ans[0] <= ans[1] || ans[0] <= ans[2] {
6+
if ans[1] >= ans[2] {
7+
ans[1] -= ans[0]
8+
} else {
9+
ans[2] -= ans[0]
10+
}
11+
ans[0]++
12+
}
13+
return ans
514
}

leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
memory1, memory2 int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 2, 2, []int{3, 1, 0}},
17+
{"TestCase2", 8, 11, []int{6, 0, 4}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.memory1, c.memory2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.memory1, c.memory2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)