Practice
Solution
Step 1: Identify missing pointer increment
When a cookie satisfies a child (s[j] ≥ g[i]), j should increment to avoid reusing the same cookie.Step 2: Check code lines
Line 5 increments count and i but does not increment j, causing the same cookie to be assigned multiple times.Final Answer:
Option A -> Option AQuick Check:
Without j increment on assignment, cookie reuse occurs [OK]
- Forgetting to sort arrays
- Incorrect loop conditions
- Not incrementing both pointers on assignment
Solution
Step 1: Identify sorting complexity
Sorting n elements takes O(n log n) comparisons.Step 2: Each comparison involves concatenating two strings of max length k and comparing them
Each comparison is O(k), so total is O(n log n * k).Final Answer:
Option D -> Option DQuick Check:
Sorting with custom comparator comparing concatenations costs O(k) per comparison [OK]
- Confusing O(n*k*log n) with O(n log n * k)
- Assuming O(n^2) due to pairwise comparisons
- Forgetting string concat cost
n. Identify the bug that causes incorrect results on some inputs.
def monotoneIncreasingDigits(n: int) -> int:
digits = list(map(int, str(n)))
marker = len(digits)
for i in range(len(digits) - 1, 0, -1):
if digits[i] < digits[i - 1]:
digits[i - 1] -= 1
marker = i
return int(''.join(map(str, digits)))
Solution
Step 1: Analyze the loop effect
The loop decrements digits[i - 1] when a violation is found and updates marker, but does not fix digits after marker.Step 2: Identify missing step to set trailing digits to 9
Without setting digits from marker to end to 9, the number may not be the largest monotone number ≤ n.Final Answer:
Option A -> Option AQuick Check:
Missing trailing digit fix leads to smaller-than-necessary result [OK]
- Forgetting to set trailing digits to 9
- Assuming one decrement fixes all violations
- Ignoring edge cases with equal digits
t total tasks and m unique tasks?Solution
Step 1: Analyze heap operations
Heap size is at mostm(unique tasks). Each task is pushed and popped at most once per execution.Step 2: Calculate total operations
Forttasks, each heap operation costs O(log m), so total is O(t log m).Final Answer:
Option A -> Option AQuick Check:
Heap size depends on unique tasks, not total tasks [OK]
- Confusing total tasks and unique tasks
- Assuming linear heap operations
- Ignoring log factor in heap push/pop
Solution
Step 1: Understand frequency decrement logic
When a task count reaches zero, it should not be pushed back into the heap.Step 2: Check condition for pushing back tasks
The conditioncnt + 1 <= 0incorrectly pushes zero counts back, causing infinite loops or extra scheduling.Final Answer:
Option A -> Option AQuick Check:
Changing tocnt + 1 < 0fixes the bug [OK]
- Pushing zero counts back causes infinite loops
- Miscounting cooldown cycles
- Incorrect loop ranges
