From 19d4291ed42bf9f3cfb760aa58c17c19241a67dc Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 12 Dec 2024 18:41:40 -0700 Subject: 2593. find score of an array after marking all elements --- find_score_arr_mark.c | 4 +--- find_score_arr_mark.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 find_score_arr_mark.py diff --git a/find_score_arr_mark.c b/find_score_arr_mark.c index fc2a402..cb5aecb 100644 --- a/find_score_arr_mark.c +++ b/find_score_arr_mark.c @@ -1,7 +1,5 @@ // 2593. Find Score of an Array After Marking All Elements -#include -#include -#include +#include "leetcode.h" /* * given an array 'nums' consisting of positive integers. starting with 'score = diff --git a/find_score_arr_mark.py b/find_score_arr_mark.py new file mode 100644 index 0000000..1fdbc06 --- /dev/null +++ b/find_score_arr_mark.py @@ -0,0 +1,33 @@ +# 2593. Find Score of an Array After Marking All Elements + +""" +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 +""" + + +class Solution(object): + def findScore(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + seen, ans = [0] * (len(nums) + 1), 0 + for i, j in sorted([i, j] for j, i in enumerate(nums)): + if seen[j]: + continue + ans += i + seen[j] = seen[j - 1] = seen[j + 1] = 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.findScore(nums=[2, 1, 3, 4, 5, 2])) + print(obj.findScore(nums=[2, 3, 5, 1, 3, 2])) -- cgit v1.2.3