Practice
Solution
Step 1: Understand problem constraints
The problem requires sorting the first array so that elements appear in the order defined by the second array, with leftover elements sorted ascending at the end.Step 2: Identify optimal approach
Counting sort using frequency arrays efficiently counts occurrences and outputs elements in the required order in O(n + k) time, where k is max element value.Final Answer:
Option D -> Option DQuick Check:
Counting sort matches problem constraints and is optimal for integer ranges [OK]
- Assuming greedy selection suffices without frequency counting
- Confusing this with subsequence or DP problems
- Using brute force nested loops which is inefficient
from typing import List
import collections
import heapq
def diagonalSort(mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
diagonals = collections.defaultdict(list)
for i in range(m):
for j in range(n):
heapq.heappush(diagonals[i - j], mat[i][j])
for i in range(m):
for j in range(n):
mat[i][j] = heapq.heappop(diagonals[i - j])
return mat
matrix = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
print(diagonalSort(matrix))Solution
Step 1: Build min-heaps keyed by diagonal (row - col)
For each element, push into heap keyed by i - j. For example, diagonal key 0 has elements [3,2,1], which heapify to [1,2,3].Step 2: Pop from heaps to replace matrix elements
Traverse matrix again, replacing each element with the smallest from its diagonal heap. This sorts each diagonal ascending.Final Answer:
Option A -> Option AQuick Check:
Diagonal keys and heaps produce sorted diagonals matching [[1,1,1,1],[1,2,2,2],[1,2,3,3]] [OK]
- Off-by-one errors in heap keys
- Mixing up heap push/pop order
- Incorrectly reconstructing matrix from heaps
nums after the while loop completes when the input is [1, 3, 2]?Solution
Step 1: Sort and find median
Sorted nums = [1, 2, 3], median = nums[(3-1)//2] = nums[1] = 2.Step 2: Apply virtual indexing and partition
Virtual indices for i=0,1,2 are (1+2*0)%3=1, (1+2*1)%3=0, (1+2*2)%3=2. - i=0: nums[1]=2 equals median -> i=1 - i=1: nums[0]=1 less than median -> swap with nums[virtual_index(right=2)]=nums[2]=3, right=1 - After swap: nums=[3,2,1], i=1, right=1 - i=1: nums[0]=3 greater than median -> swap with nums[virtual_index(left=0)]=nums[1]=2, left=1, i=2 - Final nums=[2,3,1]Final Answer:
Option A -> Option AQuick Check:
Final array satisfies wiggle property: 2 < 3 > 1 [OK]
- Confusing virtual index mapping
- Off-by-one in median calculation
- Not updating pointers correctly
Solution
Step 1: Identify condition for missing range detection
The condition 'if pointer <= curr:' incorrectly includes the case pointer == curr, causing no gap detection before first element if pointer equals first number.Step 2: Correct condition to 'if pointer < curr:'
This ensures missing ranges are detected only when pointer is strictly less than current number, capturing missing numbers before first element.Final Answer:
Option A -> Option AQuick Check:
Changing to '<' fixes missing start ranges [OK]
- Using <= instead of <
- Incorrect pointer update
- Wrong formatting of single numbers
Solution
Step 1: Understand infinite reuse implication
Since elements can be reused unlimited times, counts do not decrease; the algorithm must allow repeated placement without decrementing counts.Step 2: Modify heap usage accordingly
Never decrement counts and always push elements back to heap to maintain availability, ensuring no two adjacent elements are equal.Step 3: Evaluate other options
Removing max frequency check or resetting counts breaks correctness or efficiency; queue does not prioritize frequency.Final Answer:
Option D -> Option DQuick Check:
Maintaining counts without decrement allows infinite reuse without adjacency violation [OK]
- Removing checks without adjusting counts
- Resetting counts causing incorrect order
