Skip to content

Commit 432251e

Browse files
committed
feat: 1046. Last Stone Weight
1 parent 27ee5b8 commit 432251e

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: 1046. Last Stone Weight
3+
subtitle: "https://leetcode.com/problems/last-stone-weight/"
4+
date: 2023-12-14T17:38:00+08:00
5+
lastmod: 2023-12-14T17:38:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "1046.Last-Stone-Weight"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy, Heap, Last Stone Weight]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [1046. Last Stone Weight](https://leetcode.com/problems/last-stone-weight/)
62+
63+
## 題目
64+
You are given an array of integers stones where stones[i] is the weight of the ith stone.
65+
66+
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:
67+
68+
If x == y, both stones are destroyed, and
69+
If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
70+
At the end of the game, there is at most one stone left.
71+
72+
Return the weight of the last remaining stone. If there are no stones left, return 0.
73+
74+
75+
76+
Example 1:
77+
78+
Input: stones = [2,7,4,1,8,1]
79+
Output: 1
80+
Explanation:
81+
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
82+
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
83+
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
84+
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
85+
Example 2:
86+
87+
Input: stones = [1]
88+
Output: 1
89+
90+
91+
Constraints:
92+
93+
1 <= stones.length <= 30
94+
1 <= stones[i] <= 1000
95+
96+
## 題目大意
97+
98+
99+
## 解題思路
100+
101+
## Big O
102+
時間複雜 : ``
103+
空間複雜 : ``
104+
105+
## 來源
106+
* https://leetcode.com/problems/last-stone-weight/
107+
* https://leetcode.cn/problems/last-stone-weight/
108+
109+
## 解答
110+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/1046.Last-Stone-Weight/main.go
111+
112+
```go
113+
114+
```
115+
116+
## Benchmark
117+
118+
```sh
119+
120+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
// 時間複雜 O(), 空間複雜 O()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 string
7+
want int
8+
}{
9+
{
10+
"bbbab",
11+
4,
12+
},
13+
}
14+
15+
func TestLongestPalindromeSubseq(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
19+
t.Errorf("got = %v, want = %v", got, tt.want)
20+
}
21+
}
22+
}
23+
24+
func BenchmarkLongestPalindromeSubseq(b *testing.B) {
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
LongestPalindromeSubseq(tests[0].arg1)
28+
}
29+
}
30+
31+
/*
32+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
33+
34+
*/

0 commit comments

Comments
 (0)