Practice
def maximumUnits(boxTypes, truckSize):
boxTypes.sort(key=lambda x: x[1], reverse=True)
totalUnits = 0
for boxes, units in boxTypes:
if truckSize == 0:
break
take = min(boxes, truckSize)
totalUnits += take * units
truckSize -= take
return totalUnits
boxTypes = [[1,3],[2,2],[3,1]]
truckSize = 4
print(maximumUnits(boxTypes, truckSize))
Solution
Step 1: Sort boxTypes by units descending
Sorted list: [[1,3],[2,2],[3,1]] (already sorted)Step 2: Iterate and pick boxes until truckSize=0
Take 1 box with 3 units -> totalUnits=3, truckSize=3 left; take 2 boxes with 2 units -> totalUnits=3+4=7, truckSize=1 left; take 1 box with 1 unit -> totalUnits=7+1=8, truckSize=0 stop.Final Answer:
Option B -> Option BQuick Check:
Sum matches manual calculation [OK]
- Off-by-one in take calculation
- Not stopping when truckSize=0
- Incorrect sorting order
Solution
Step 1: Identify candidate values from the first domino
The only possible uniform values are the top or bottom value of the first domino, since all dominoes must match one of these.Step 2: Check feasibility and count rotations
For each candidate, verify if all dominoes can be rotated to match it and count minimal rotations needed.Final Answer:
Option B -> Option BQuick Check:
Checking only two candidates reduces complexity and guarantees correctness [OK]
- Trying all numbers 1-6 unnecessarily
- Using DP or backtracking wasting time
n with k unique characters?Solution
Step 1: Identify heap operations per character
Each character is pushed and popped at most once per occurrence, total n operations.Step 2: Analyze heap size and operation cost
Heap size is at most k (unique chars), each push/pop is O(log k), so total O(n log k).Final Answer:
Option B -> Option BQuick Check:
Heap operations dominate, not scanning entire string [OK]
- Confusing n and k in complexity
- Assuming linear time without heap cost
- Mistaking quadratic due to nested loops
Solution
Step 1: Understand new requirement
Children with equal ratings do not require more candies than each other, only strictly higher ratings require more candies.Step 2: Adjust comparisons in algorithm
Keep '>' comparisons to ensure only strictly higher ratings get more candies; do not update candies when ratings are equal.Step 3: Avoid changing to '>=' which would incorrectly increase candies for equal ratings
Changing to '>=' breaks minimality and correctness.Final Answer:
Option C -> Option CQuick Check:
Strict '>' comparisons preserve problem constraints with equal ratings [OK]
- Changing '>' to '>=' causing over-assignment
- Initializing candies with zeros
- Using sorting unnecessarily
Solution
Step 1: Understand reuse impact
If sticks can be reused infinitely, merges do not reduce the number of sticks, so the problem changes fundamentally.Step 2: Analyze problem feasibility
Because sticks remain after merging, the process can continue indefinitely, making minimum total cost undefined or infinite.Final Answer:
Option C -> Option CQuick Check:
Reuse breaks the problem constraints -> no minimal cost solution [OK]
- Assuming min-heap still works without removing merged sticks
- Trying to sort once ignoring dynamic merges
- Attempting DP without problem redefinition
