-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1062 from 0xff-dev/2593
Add solution and test-cases for problem 2593
- Loading branch information
Showing
3 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...code/2501-2600/2593.Find-Score-of-an-Array-After-Marking-All-Elements/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# [2593.Find Score of an Array After Marking All Elements][title] | ||
|
||
## Description | ||
You are given an array `nums` consisting of positive integers. | ||
|
||
Starting with `score = 0`, apply the following algorithm: | ||
|
||
- Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index. | ||
- Add the value of the chosen integer to `score`. | ||
- Mark **the chosen element and its two adjacent elements if they exist**. | ||
- Repeat until all the array elements are marked. | ||
|
||
Return the score you get after applying the above algorithm. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: nums = [2,1,3,4,5,2] | ||
Output: 7 | ||
Explanation: We mark the elements as follows: | ||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2]. | ||
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2]. | ||
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2]. | ||
Our score is 1 + 2 + 4 = 7. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: nums = [2,3,5,1,3,2] | ||
Output: 5 | ||
Explanation: We mark the elements as follows: | ||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2]. | ||
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2]. | ||
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2]. | ||
Our score is 1 + 2 + 2 = 5. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
56 changes: 55 additions & 1 deletion
56
leetcode/2501-2600/2593.Find-Score-of-an-Array-After-Marking-All-Elements/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,59 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
import "container/heap" | ||
|
||
type item2593 struct { | ||
v, i int | ||
} | ||
|
||
type heap2593 []item2593 | ||
|
||
func (h *heap2593) Len() int { | ||
return len(*h) | ||
} | ||
|
||
func (h *heap2593) Swap(i, j int) { | ||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i] | ||
} | ||
func (h *heap2593) Less(i, j int) bool { | ||
a, b := (*h)[i], (*h)[j] | ||
if a.v == b.v { | ||
return a.i < b.i | ||
} | ||
return a.v < b.v | ||
} | ||
|
||
func (h *heap2593) Push(x any) { | ||
*h = append(*h, x.(item2593)) | ||
} | ||
func (h *heap2593) Pop() any { | ||
old := *h | ||
l := len(old) | ||
x := old[l-1] | ||
*h = old[:l-1] | ||
return x | ||
} | ||
|
||
func Solution(nums []int) int64 { | ||
l := len(nums) | ||
used := make([]bool, l) | ||
h := &heap2593{} | ||
for i := range l { | ||
heap.Push(h, item2593{v: nums[i], i: i}) | ||
} | ||
ans := int64(0) | ||
for h.Len() > 0 { | ||
top := heap.Pop(h).(item2593) | ||
if used[top.i] { | ||
continue | ||
} | ||
ans += int64(top.v) | ||
if top.i-1 >= 0 { | ||
used[top.i-1] = true | ||
} | ||
if top.i+1 < l { | ||
used[top.i+1] = true | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters