LeetCode 2593. 标记所有元素后数组的分数 C#
原题链接
中等
作者:
hpstory
,
2023-03-19 14:25:51
,
所有人可见
,
阅读 32
C# 代码
public class Solution {
public long FindScore(int[] nums) {
long result = 0;
int n = nums.Length;
int[] index = new int[n];
for (int i = 0; i < n; i++) index[i] = i;
Array.Sort(index, (x, y) => {
if (nums[x] == nums[y]) return x - y;
return nums[x] - nums[y];
});
bool[] used = new bool[n];
for (int i = 0; i < n; i++){
if (!used[index[i]]){
result += nums[index[i]];
if (index[i] > 0) used[index[i] - 1] = true;
if (index[i] < n - 1) used[index[i] + 1] = true;
}
}
return result;
}
}