topic:
6351. The scores of the array after the marking all elements.md
Give you an array nums ,It contains several positive integers。
Start score score = 0 ,Please find the final score according to the algorithm below:
Select the smallest and not marked integer from the array。If there are equal elements,Choose one with the smallest bidding。
Add the selected integer to score middle。
mark 被选middle元素,If there are adjacent elements,则同时mark Two elements adjacent to it 。
重复此过程直到数组middle所有元素都被mark。
Please return to the final score after executing the above algorithm。
Exemplary example 1:
enter:nums = [2,1,3,4,5,2]
Output:7
explain:我们按照如下步骤mark元素:
- 1 是最小未mark元素,所以mark它和相邻两个元素:[2,1,3,4,5,2] 。
- 2 是最小未mark元素,所以mark它和左边相邻元素:[2,1,3,4,5,2] 。
- 4 是仅剩唯一未mark的元素,所以我们mark它:[2,1,3,4,5,2] 。
Total score 1 + 2 + 4 = 7 。Thought:
Direct violence,Then it’s timeout,But I feel like the answer is quite violent。Code:
I am 1
2
3
4
5
6
7
8
9
10
11
12
13class Solution:
def findScore(self, nums: List[int]) -> int:
while set(nums) != {inf}:
print(nums)
mm = min(nums)
min_index = nums.index(mm)
grade += mm
nums[min_index] = inf
if min_index > 0:
nums[min_index - 1] = inf
if min_index < len(nums) - 1:
nums[min_index + 1] = inf
return grade
1 | class Solution: |