Practice
Solution
Step 1: Understand the problem constraints
The increment operation must be efficient even when called multiple times, so directly incrementing bottom k elements each time (O(n*k)) is too slow.Step 2: Recognize the lazy increment pattern
Using an auxiliary array to store increments lazily and applying them during pop operations reduces total time to O(n), as increments are propagated only once per element.Final Answer:
Option C -> Option CQuick Check:
Lazy increments avoid repeated updates, achieving O(n) total time [OK]
- Directly incrementing elements each time causes TLE
- Using complex data structures unnecessarily
Solution
Step 1: Understand the problem constraints
The system must support fast posting and retrieving the top 10 recent tweets from a user's followees including themselves.Step 2: Evaluate approaches
Brute force scanning or sorting all tweets each time is inefficient. DP is not applicable here. Using linked lists per user and a max heap for merging recent tweets achieves O(1) posting and O(k log F) retrieval, where F is followees count.Final Answer:
Option B -> Option BQuick Check:
Linked list + hashmap + heap is the optimal pattern for this problem [OK]
- Thinking sorting all tweets each time is efficient
- Trying DP for a streaming feed problem
Solution
Step 1: Insert 1
Low heap empty, push -1 to low; low_size=1, high_size=0.Step 2: Insert 5
5 > -low[0] (which is 1), push 5 to high; low_size=1, high_size=1; heaps balanced.Step 3: Insert 3
3 <= 5 but > 1, push 3 to high; high_size=2, low_size=1; balance heaps by moving smallest from high (3) to low (-3); low_size=2, high_size=1.Step 4: Find median
low_size > high_size, median is -low[0] = 3.0.Step 5: Re-examine median calculation
After balancing, low heap has [-3, -1], high heap has [5]. Median is top of low heap = 3.0, but the question asks for median after adding 1,5,3 in order, so median is 3.0.Final Answer:
Option B -> Option BQuick Check:
Median after [1,5,3] is 3.0 [OK]
- Forgetting to balance heaps after insertion
- Returning wrong heap top for median
Solution
Step 1: Understand problem requirements
The data structure must efficiently support set, snap, and get operations with many snapshots and updates.Step 2: Evaluate approaches
Storing full snapshots (B) uses too much space and is slow for many snaps. A global hash map with linear search (C) makes get operations slow. Segment trees (D) are complex and not optimal here. Using a hash map per index with binary search (A) allows O(1) amortized set and snap, and O(log m) get, balancing time and space efficiently.Final Answer:
Option B -> Option BQuick Check:
Binary search per index enables fast retrieval [OK]
- Thinking full snapshots are efficient for many snaps
- Using linear search for get causes slow queries
Solution
Step 1: Analyze addNum complexity
Adding a number involves pushing to a heap and balancing heaps, each O(log n) operations.Step 2: Analyze findMedian complexity
Median is retrieved from the top of heaps without modification, O(1) time.Final Answer:
Option C -> Option CQuick Check:
Insertion is O(log n), median retrieval O(1) [OK]
- Assuming sorting each query is O(log n)
- Confusing addNum with findMedian complexity
