From 02054954a8f647d541aa29372b52f9e863e5b31a Mon Sep 17 00:00:00 2001 From: Siddhartha Dutta Date: Fri, 13 Dec 2024 11:05:27 +0000 Subject: [PATCH] Dec 13 --- ... of an Array After Marking All Elements.py | 32 +++++++++++++++++++ .../README.md | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 2024-12-December-LeetCoding-Challenge/Find Score of an Array After Marking All Elements.py diff --git a/2024-12-December-LeetCoding-Challenge/Find Score of an Array After Marking All Elements.py b/2024-12-December-LeetCoding-Challenge/Find Score of an Array After Marking All Elements.py new file mode 100644 index 0000000..4f80da4 --- /dev/null +++ b/2024-12-December-LeetCoding-Challenge/Find Score of an Array After Marking All Elements.py @@ -0,0 +1,32 @@ +from typing import List + + +class Solution: + def findScore(self, nums: List[int]) -> int: + nums_idx = [(num, idx) for idx, num in enumerate(nums)] + nums_idx.sort(reverse=True) + score = 0 + marked = set() + while nums_idx: + num, idx = nums_idx.pop() + if idx in marked: + continue + score += num + marked.add(idx) + if idx > 0: + marked.add(idx-1) + if idx < len(nums)-1: + marked.add(idx+1) + return score + + +def main(): + nums = [2, 1, 3, 4, 5, 2] + assert Solution().findScore(nums) == 7 + + nums = [2, 3, 5, 1, 3, 2] + assert Solution().findScore(nums) == 5 + + +if __name__ == '__main__': + main() diff --git a/2024-12-December-LeetCoding-Challenge/README.md b/2024-12-December-LeetCoding-Challenge/README.md index 5e1cfe5..a7c3b3d 100644 --- a/2024-12-December-LeetCoding-Challenge/README.md +++ b/2024-12-December-LeetCoding-Challenge/README.md @@ -15,7 +15,7 @@ | December 10 | [2981. Find Longest Special Substring That Occurs Thrice I](https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/) | Medium | Unsolved | | December 11 | [2779. Maximum Beauty of an Array After Applying Operation](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/) | Medium | Unsolved | | December 12 | [2558. Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile/) | Easy | Solved | -| December 13 | []() | | | +| December 13 | [2593. Find Score of an Array After Marking All Elements](https://leetcode.com/problems/find-score-of-an-array-after-marking-all-elements/) | Medium | Solved | | December 14 | []() | | | | December 15 | []() | | | | December 16 | []() | | | @@ -39,5 +39,5 @@ | Level | Problems | Solved | Unsolved | | --- | --- | --- | --- | | Easy | 3 | 3 | 0 | -| Medium | 9 | 6 | 3 | +| Medium | 10 | 7 | 3 | | Hard | 0 | 0 | 0 |