diff options
author | Jack Sangdahl <[email protected]> | 2024-12-12 18:41:40 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-12-12 18:41:49 -0700 |
commit | 19d4291ed42bf9f3cfb760aa58c17c19241a67dc (patch) | |
tree | 895af93ebb866116db6f81dcc5d03055b3a735d1 /find_score_arr_mark.py | |
parent | 3da02bd52f9772af30f9336da114b7f37e8a5025 (diff) | |
download | leetcode-19d4291ed42bf9f3cfb760aa58c17c19241a67dc.tar.gz leetcode-19d4291ed42bf9f3cfb760aa58c17c19241a67dc.zip |
2593. find score of an array after marking all elements
Diffstat (limited to 'find_score_arr_mark.py')
-rw-r--r-- | find_score_arr_mark.py | 33 |
1 files changed, 33 insertions, 0 deletions
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])) |