Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35a05e0

Browse files
committed
Merge pull request #388 from 0xff-dev/1962
Add solution and test-cases for problem 1962
2 parents fea22f8 + 276e571 commit 35a05e0

File tree

3 files changed

+91
-25
lines changed

3 files changed

+91
-25
lines changed

‎leetcode/1901-2000/1962.Remove-Stones-to-Minimize-the-Total/README.md‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1962.Remove Stones to Minimize the Total][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 a **0-indexed** integer array `piles`, where `piles[i]` represents the number of stones in the i<sup>th</sup> pile, and an integer `k`. You should apply the following operation **exactly** `k` times:
5+
6+
- Choose any `piles[i]` and **remove** `floor(piles[i] / 2)` stones from it.
7+
8+
**Notice** that you can apply the operation on the **same** pile more than once.
9+
10+
Return the **minimum** possible total number of stones remaining after applying the `k` operations.
11+
12+
`floor(x)` is the **greatest** integer that is **smaller** than or **equal** to `x` (i.e., rounds `x` down).
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: piles = [5,4,9], k = 2
18+
Output: 12
19+
Explanation: Steps of a possible scenario are:
20+
- Apply the operation on pile 2. The resulting piles are [5,4,5].
21+
- Apply the operation on pile 0. The resulting piles are [3,4,5].
22+
The total number of stones in [3,4,5] is 12.
1323
```
1424

15-
## 题意
16-
> ...
17-
18-
## 题解
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Remove Stones to Minimize the Total
23-
```go
2427
```
25-
28+
Input: piles = [4,3,6,7], k = 3
29+
Output: 12
30+
Explanation: Steps of a possible scenario are:
31+
- Apply the operation on pile 2. The resulting piles are [4,3,3,7].
32+
- Apply the operation on pile 3. The resulting piles are [4,3,3,4].
33+
- Apply the operation on pile 0. The resulting piles are [2,3,3,4].
34+
The total number of stones in [2,3,3,4] is 12.
35+
```
2636

2737
## 结语
2838

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
)
6+
7+
type Piles []int
8+
9+
func (p *Piles) Len() int {
10+
return len(*p)
11+
}
12+
13+
func (p *Piles) Less(i, j int) bool {
14+
a := (*p)[i]
15+
b := (*p)[j]
16+
if a&1 == 1 {
17+
a++
18+
}
19+
if b&1 == 1 {
20+
b++
21+
}
22+
23+
return (*p)[i]-a/2 > (*p)[j]-b/2
24+
}
25+
26+
func (p *Piles) Swap(i, j int) {
27+
(*p)[i], (*p)[j] = (*p)[j], (*p)[i]
28+
}
29+
30+
func (p *Piles) Push(x interface{}) {
31+
*p = append(*p, x.(int))
32+
}
33+
34+
func (p *Piles) Pop() interface{} {
35+
old := *p
36+
l := len(old)
37+
x := old[l-1]
38+
*p = old[:l-1]
439
return x
540
}
41+
42+
func Solution(piles []int, k int) int {
43+
sum := 0
44+
for _, p := range piles {
45+
sum += p
46+
}
47+
h := Piles(piles)
48+
heap.Init(&h)
49+
for ; k > 0 && h.Len() > 0; k-- {
50+
x := heap.Pop(&h).(int)
51+
y := x
52+
if x&1 == 1 {
53+
x--
54+
}
55+
sum -= x / 2
56+
heap.Push(&h, y-x/2)
57+
}
58+
59+
return sum
60+
}

‎leetcode/1901-2000/1962.Remove-Stones-to-Minimize-the-Total/Solution_test.go‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 4, 9}, 2, 12},
18+
{"TestCase2", []int{4, 3, 6, 7}, 3, 12},
19+
{"TestCase3", []int{10, 88, 74, 63, 28, 99, 131, 74, 97, 201, 383, 274}, 6, 865},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /