Practice
def merge(nums1, m, nums2, n):
i = m - 1
j = n - 1
k = m + n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[k] = nums1[i]
i -= 1
else:
nums1[k] = nums2[j]
j -= 1
k -= 1
while j >= 0:
nums1[k] = nums2[j]
j -= 1
k -= 1
nums1 = [2,5,7,0,0,0]
m = 3
nums2 = [1,3,6]
n = 3
merge(nums1, m, nums2, n)
print(nums1)Solution
Step 1: Trace last elements comparison
Compare nums1[2]=7 and nums2[2]=6, place 7 at nums1[5], decrement i and k.Step 2: Continue merging backwards
Next compare nums1[1]=5 and nums2[2]=6, place 6 at nums1[4], decrement j and k. Then place 5, 3, 2, 1 accordingly.Final Answer:
Option D -> Option DQuick Check:
Final merged array is sorted and contains all elements [OK]
- Off-by-one errors in indices
- Not copying remaining nums2 elements
write_index after processing the input [1,1,2]?Solution
Step 1: Trace write_index and read_index for input [1,1,2]
Initially, write_index=0. Iteration 0: write_index<2, write 1, write_index=1. Iteration 1: write_index=1<2, write 1, write_index=2. Iteration 2: nums[2]=2 != nums[0]=1, write 2, write_index=3.Step 2: Final write_index value
After processing all elements, write_index=3, indicating the length of the modified array.Final Answer:
Option C -> Option CQuick Check:
All elements allowed, write_index matches array length [OK]
- Off-by-one errors in write_index
- Confusing read_index with write_index
- Not handling first two elements separately
Solution
Step 1: Understand the problem constraints
The input array is sorted but contains negative numbers, so squaring can disrupt order.Step 2: Identify the approach that leverages sorted input
Two pointers from both ends compare absolute values and fill the output array from the end, ensuring sorted squares in O(n) time.Final Answer:
Option D -> Option DQuick Check:
Two-pointer approach uses input order optimally [OK]
- Assuming squaring preserves order
- Using sorting after squaring without leveraging input order
- Confusing DP with this problem
Solution
Step 1: Understand problem constraints
The problem requires calculating trapped water efficiently in linear time and constant space.Step 2: Identify approach matching constraints
The two pointers approach moves inward from both ends, updating left_max and right_max, allowing O(n) time and O(1) space.Final Answer:
Option A -> Option AQuick Check:
Two pointers approach is known optimal for trapping rain water [OK]
- Assuming brute force is efficient enough
- Confusing DP with 2D tables for this problem
- Thinking greedy fill lowest bar always works
Solution
Step 1: Understand the generalization
Allowing at most k duplicates means we must check if the current element differs from the element k positions before the write pointer.Step 2: Modify the condition accordingly
Replace the fixed 2 with k in the condition: write_index < k or nums[read_index] != nums[write_index - k]. This generalizes the approach.Step 3: Avoid inefficient or incorrect methods
Hash maps or nested loops increase time or space complexity unnecessarily.Final Answer:
Option A -> Option AQuick Check:
Condition generalized to k duplicates ensures correctness and efficiency [OK]
- Not generalizing condition to k
- Using hash maps increases space
- Nested loops cause O(n²) time
