2655. Rearrange Array to Maximize Prefix Score
My accepted Python solution to LeetCode problem 2655, Rearrange Array to Maximize Prefix Score, running in 91ms.
- Difficulty: Medium
- Python
- Runtime 91ms
- Memory 29.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 91ms, memory 29.5MB, accepted 2025-12-31.
class Solution:
def maxScore(self, nums: List[int]) -> int:
# Sort in descending order to maximize prefix sums
nums.sort(reverse=True)
prefix_sum = 0
count = 0
for num in nums:
prefix_sum += num
if prefix_sum > 0:
count += 1
else:
break
return count