-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94f1526
commit 0205495
Showing
2 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
2024-12-December-LeetCoding-Challenge/Find Score of an Array After Marking All Elements.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters