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 an array maximumHeight, where maximumHeight[i] denotes the maximum height the ith tower can be assigned.
Your task is to assign a height to each tower so that:
ith tower is a positive integer and does not exceed maximumHeight[i].Return the maximum possible total sum of the tower heights. If it's not possible to assign heights, return -1.
Example 1:
Input: maximumHeight = [2,3,4,3]
Output: 10
Explanation:
We can assign heights in the following way: [1, 2, 4, 3].
Example 2:
Input: maximumHeight = [15,10]
Output: 25
Explanation:
We can assign heights in the following way: [15, 10].
Example 3:
Input: maximumHeight = [2,2,1]
Output: -1
Explanation:
It's impossible to assign positive heights to each index so that no two towers have the same height.
Constraints:
1 <= maximumHeight.length <= 1051 <= maximumHeight[i] <= 109Problem summary: You are given an array maximumHeight, where maximumHeight[i] denotes the maximum height the ith tower can be assigned. Your task is to assign a height to each tower so that: The height of the ith tower is a positive integer and does not exceed maximumHeight[i]. No two towers have the same height. Return the maximum possible total sum of the tower heights. If it's not possible to assign heights, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[2,3,4,3]
[15,10]
[2,2,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
class Solution {
public long maximumTotalSum(int[] maximumHeight) {
long ans = 0;
int mx = 1 << 30;
Arrays.sort(maximumHeight);
for (int i = maximumHeight.length - 1; i >= 0; --i) {
int x = Math.min(maximumHeight[i], mx - 1);
if (x <= 0) {
return -1;
}
ans += x;
mx = x;
}
return ans;
}
}
// Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
func maximumTotalSum(maximumHeight []int) int64 {
slices.SortFunc(maximumHeight, func(a, b int) int { return b - a })
ans := int64(0)
mx := 1 << 30
for _, x := range maximumHeight {
x = min(x, mx-1)
if x <= 0 {
return -1
}
ans += int64(x)
mx = x
}
return ans
}
# Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
class Solution:
def maximumTotalSum(self, maximumHeight: List[int]) -> int:
maximumHeight.sort()
ans, mx = 0, inf
for x in maximumHeight[::-1]:
x = min(x, mx - 1)
if x <= 0:
return -1
ans += x
mx = x
return ans
// Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
// class Solution {
// public long maximumTotalSum(int[] maximumHeight) {
// long ans = 0;
// int mx = 1 << 30;
// Arrays.sort(maximumHeight);
// for (int i = maximumHeight.length - 1; i >= 0; --i) {
// int x = Math.min(maximumHeight[i], mx - 1);
// if (x <= 0) {
// return -1;
// }
// ans += x;
// mx = x;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3301: Maximize the Total Height of Unique Towers
function maximumTotalSum(maximumHeight: number[]): number {
maximumHeight.sort((a, b) => a - b).reverse();
let ans: number = 0;
let mx: number = Infinity;
for (let x of maximumHeight) {
x = Math.min(x, mx - 1);
if (x <= 0) {
return -1;
}
ans += x;
mx = x;
}
return ans;
}
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.