Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the given order).
Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it. The score of nums is the number of positive integers in the array prefix.
Return the maximum score you can achieve.
Example 1:
Input: nums = [2,-1,0,1,-3,3,-3] Output: 6 Explanation: We can rearrange the array into nums = [2,3,1,-1,-3,0,-3]. prefix = [2,5,6,5,2,2,-1], so the score is 6. It can be shown that 6 is the maximum score we can obtain.
Example 2:
Input: nums = [-2,-3,0] Output: 0 Explanation: Any rearrangement of the array will result in a score of 0.
Constraints:
1 <= nums.length <= 105-106 <= nums[i] <= 106Problem summary: You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the given order). Let prefix be the array containing the prefix sums of nums after rearranging it. In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it. The score of nums is the number of positive integers in the array prefix. Return the maximum score you can achieve.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[2,-1,0,1,-3,3,-3]
[-2,-3,0]
two-city-scheduling)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2587: Rearrange Array to Maximize Prefix Score
class Solution {
public int maxScore(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
long s = 0;
for (int i = 0; i < n; ++i) {
s += nums[n - i - 1];
if (s <= 0) {
return i;
}
}
return n;
}
}
// Accepted solution for LeetCode #2587: Rearrange Array to Maximize Prefix Score
func maxScore(nums []int) int {
sort.Ints(nums)
n := len(nums)
s := 0
for i := range nums {
s += nums[n-i-1]
if s <= 0 {
return i
}
}
return n
}
# Accepted solution for LeetCode #2587: Rearrange Array to Maximize Prefix Score
class Solution:
def maxScore(self, nums: List[int]) -> int:
nums.sort(reverse=True)
s = 0
for i, x in enumerate(nums):
s += x
if s <= 0:
return i
return len(nums)
// Accepted solution for LeetCode #2587: Rearrange Array to Maximize Prefix Score
impl Solution {
pub fn max_score(mut nums: Vec<i32>) -> i32 {
nums.sort_by(|a, b| b.cmp(a));
let mut s: i64 = 0;
for (i, &x) in nums.iter().enumerate() {
s += x as i64;
if s <= 0 {
return i as i32;
}
}
nums.len() as i32
}
}
// Accepted solution for LeetCode #2587: Rearrange Array to Maximize Prefix Score
function maxScore(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let s = 0;
for (let i = 0; i < n; ++i) {
s += nums[n - i - 1];
if (s <= 0) {
return i;
}
}
return n;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.