aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-12 18:41:40 -0700
committerJack Sangdahl <[email protected]>2024-12-12 18:41:49 -0700
commit19d4291ed42bf9f3cfb760aa58c17c19241a67dc (patch)
tree895af93ebb866116db6f81dcc5d03055b3a735d1
parent3da02bd52f9772af30f9336da114b7f37e8a5025 (diff)
downloadleetcode-19d4291ed42bf9f3cfb760aa58c17c19241a67dc.tar.gz
leetcode-19d4291ed42bf9f3cfb760aa58c17c19241a67dc.zip
2593. find score of an array after marking all elements
-rw-r--r--find_score_arr_mark.c4
-rw-r--r--find_score_arr_mark.py33
2 files changed, 34 insertions, 3 deletions
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 <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
+#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]))