avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

6351. The scores of the array after the marking all elements

  2024-01-01        
字数统计: 348字   |   阅读时长: 1min

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
13
class 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
Spirit Sorting
1
2
3
4
5
6
7
8
9
10
class Solution:
def findScore(self, nums: List[int]) -> int:
ans = 0
vis = [False] * (len(nums) + 2) # Guarantee the bidding does not cross the boundary
for i, x in sorted(enumerate(nums, 1), key=lambda p: p[1]):
if not vis[i]:
vis[i - 1] = True
vis[i + 1] = True # mark相邻的两个元素
ans += x
return ans
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
6347Number of vowel string within the scope of statistics
76Minimum cover string
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号