Practice
Solution
Step 1: Trace left-to-right pass
ratings = [1,0,2], candies init = [1,1,1] - i=1: ratings[1]=0 < ratings[0]=1, no change - i=2: ratings[2]=2 > ratings[1]=0, candies[2] = candies[1]+1 = 2 candies after pass: [1,1,2]Step 2: Trace right-to-left pass
i=1: ratings[1]=0 < ratings[2]=2, no change i=0: ratings[0]=1 > ratings[1]=0 and candies[0]=1 <= candies[1]=1, update candies[0] = candies[1]+1 = 2 candies after pass: [2,1,2] Sum = 2+1+2 = 5Final Answer:
Option B -> Option BQuick Check:
Sum matches expected output 5 [OK]
- Forgetting to update candies in right-to-left pass
- Off-by-one errors in loops
- Misreading comparison operators
max_platforms after processing the second train (index 1)?Solution
Step 1: Sort trains by arrival time
Sorted trains: [(900, 910), (940, 1200), (950, 1120)]Step 2: Process trains up to index 1
After first train: heap=[910], max_platforms=1 Second train arrival=940, heap top=910 ≤ 940, pop 910 Push 1200, heap=[1200], max_platforms=max(1,1)=1 Since question asks after second train, max_platforms=1Final Answer:
Option B -> Option BQuick Check:
Heap size after second train is 1, max_platforms updated to 1 [OK]
- Not popping from heap before push
- Confusing max_platforms update timing
- Off-by-one in iteration
Solution
Step 1: Review the merge loop
The loop pops two smallest sticks and sums their lengths as cost.Step 2: Check if merged stick is reinserted
The merged stick must be pushed back into the heap to continue merging. Missing this causes infinite loop or incorrect cost.Final Answer:
Option D -> Option DQuick Check:
Without pushing merged stick, heap shrinks incorrectly -> infinite loop or wrong result [OK]
- Forgetting to push merged stick back
- Incorrect base case handling
- Misordering heap operations
n, where d is the number of digits in n?Solution
Step 1: Understand the relationship between digits and input size
The number of digits d in n is proportional to log10 n.Step 2: Express complexity in terms of n
The algorithm runs in O(d) time, which is O(log n) when expressed in terms of n.Final Answer:
Option B -> Option BQuick Check:
Expressing complexity in terms of n, O(log n) is correct and more standard [OK]
- Confusing input size n with digit count d
- Assuming repeated decrements cause quadratic time
- Ignoring that digit count is log n
def partitionLabels(s):
last = [0] * 26
for i, c in enumerate(s):
last[ord(c) - ord('a')] = i
res = []
start = 0
end = 0
for i, c in enumerate(s):
end = last[ord(c) - ord('a')] # Bug here: missing max()
if i == end:
res.append(end - start + 1)
start = i + 1
return res
Solution
Step 1: Understand role of end variable
End must track the furthest last occurrence of any character seen so far to avoid premature partitioning.Step 2: Identify bug in updating end
Assigning end directly to last occurrence of current character without max() loses track of previous furthest last occurrence, causing incorrect partitions.Final Answer:
Option D -> Option DQuick Check:
Missing max() causes partitions to cut too early [OK]
- Forgetting max() when updating partition end
- Off-by-one errors in partition size calculation
- Not precomputing last occurrences
