|
| 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 | +``` |
0 commit comments