Practice
import heapq
def minimumDeviation(nums):
heap = []
min_val = float('inf')
for num in nums:
if num % 2 == 1:
num *= 2
min_val = min(min_val, num)
heapq.heappush(heap, -num) # max heap by pushing negative
min_dev = float('inf')
while True:
max_val = -heapq.heappop(heap)
min_dev = min(min_dev, max_val - min_val)
if max_val % 2 == 0:
max_val //= 2
min_val = min(min_val, max_val)
heapq.heappush(heap, -max_val)
else:
break
return min_dev
print(minimumDeviation([1,2,3,4]))Solution
Step 1: Normalize input and build max-heap
Input [1,2,3,4] becomes [2,2,6,4] after doubling odds. min_val = 2. Heap = [-6,-4,-2,-2].Step 2: Iteratively reduce max and update min_dev
Pop max=6, min_dev=6-2=4. Halve 6 -> 3, min_val=2, push -3. Pop max=4, min_dev=4-2=2. Halve 4 -> 2, min_val=2, push -2. Pop max=3, min_dev=2-2=0. 3 is odd, stop. Minimum deviation found is 1 (from earlier step).Final Answer:
Option A -> Option AQuick Check:
Final returned value matches expected minimal deviation [OK]
- Forgetting to double odd numbers initially
- Stopping too early without halving max even values
- Miscomputing min_val after halving
rearrangeString("aabc", 2)?Solution
Step 1: Trace first iterations of the loop
Initial freq: {a:2, b:1, c:1}, max_heap=[(-2,'a'),(-1,'b'),(-1,'c')]. Pop 'a', res=['a'], queue=[('a',-1)]. Queue size < k=2, continue.Step 2: Next iterations and final string
Pop 'b', res=['a','b'], queue=[('a',-1),('b',0)]. Queue size == k, pop front ('a',-1), since count<0, push back (-1,'a'). Next pop 'a', res=['a','b','a'], queue=[('b',0),('a',0)]. Pop front ('b',0), count not <0, no push. Pop 'c', res=['a','b','a','c'], queue=[('a',0),('c',0)]. Final string length matches input length.Final Answer:
Option A -> Option AQuick Check:
Output "abac" respects k=2 spacing and uses all chars [OK]
- Confusing when to push back characters into heap
- Off-by-one in decrementing counts
- Returning partial strings without length check
Solution
Step 1: Analyze median calculation for even k
Median for even k is average of two middle elements, so sum must be divided by 2.Step 2: Identify bug
Line returning sum without division causes median to be twice the correct value.Final Answer:
Option A -> Option AQuick Check:
Missing division by 2 in even median calculation [OK]
- Forgetting to divide sum by 2 for even k median
- Incorrect index calculation for median
- Wrong removal or insertion causing imbalance
Solution
Step 1: Analyze heap operations
Building two heaps of size n takes O(n) each, but heap push/pop operations during processing take O(log n) each.Step 2: Count total operations
Each interval is pushed and popped at most once from each heap, resulting in O(n log n) total time.Final Answer:
Option A -> Option AQuick Check:
Heap operations dominate complexity, confirming O(n log n) [OK]
- Assuming linear time due to single pass
- Confusing n and k in complexity
- Mistaking nested loops for heap operations
Solution
Step 1: Understand the new constraints
Cooldown is zero, so tasks can be scheduled consecutively, but frequency limits restrict how many times each task can run.Step 2: Identify correct scheduling approach
Using a max-heap to pick the task with highest remaining frequency ensures tasks are scheduled optimally without cooldown constraints.Step 3: Why other options fail
Ignoring frequency leads to over-scheduling; hash map cooldown tracking is unnecessary; simulating day-by-day is inefficient.Final Answer:
Option D -> Option DQuick Check:
Max-heap prioritizes tasks by frequency -> [OK]
- Ignoring frequency constraints
- Using cooldown tracking when space=0
- Simulating day-by-day unnecessarily
