Skip to content

Add solution and test-cases for problem 1860 #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions leetcode/1801-1900/1860.Incremental-Memory-Leak/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
13 changes: 11 additions & 2 deletions leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 10 additions & 11 deletions leetcode/1801-1900/1860.Incremental-Memory-Leak/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading