Height Checker — LeetCode #1051 (Easy)
Tags: Array, Sorting, Counting Sort
Related patterns: Counting Sort, Array
Brute Force approach
Time complexity: O(n²). Space complexity: O(n).
We can simply sort the heights to find the expected order and then compare each student's height with the expected height. This is straightforward but inefficient.
The sorting step takes O(n log n) time, and the comparison takes O(n), making the overall complexity O(n log n). However, if we consider the comparison as part of the sorting, we can say it's O(n²) in a naive implementation.
- Step 1: Create a copy of the heights array and sort it to get the expected order.
- Step 2: Initialize a counter to zero to keep track of mismatches.
- Step 3: Iterate through the original heights array and the sorted array simultaneously, incrementing the counter whenever the heights do not match.
1. heights = [1, 1, 4, 2, 1, 3], expected = sorted(heights) = [1, 1, 1, 2, 3, 4]
2. Initialize count = 0.
3. Compare heights[0] (1) with expected[0] (1) -> match, count = 0.
4. Compare heights[1] (1) with expected[1] (1) -> match, count = 0.
5. Compare heights[2] (4) with expected[2] (1) -> mismatch, count = 1.
6. Compare heights[3] (2) with expected[3] (2) -> match, count = 1.
7. Compare heights[4] (1) with expected[4] (3) -> mismatch, count = 2.
8. Compare heights[5] (3) with expected[5] (4) -> mismatch, count = 3.
Final count = 3.
Optimal Solution approach
Time complexity: O(n). Space complexity: O(1).
Instead of sorting the array, we can count the occurrences of each height and then determine how many heights are out of place based on their expected positions. This is more efficient.
The counting step runs in O(n) time, and since the count array size is constant (101), the space complexity is O(1).
- Step 1: Create a count array of size 101 (since heights range from 1 to 100) to count occurrences of each height.
- Step 2: Populate the count array by iterating through the heights array.
- Step 3: Initialize a counter and iterate through the count array to determine the expected heights and compare with the original heights, counting mismatches.
1. heights = [1, 1, 4, 2, 1, 3], count = [0, 3, 1, 1, 1, 0, ..., 0].
2. Initialize mismatches = 0, index = 0.
3. For height = 1: count[1] = 3, check heights[0] (1) -> match, index = 1; check heights[1] (1) -> match, index = 2; check heights[2] (4) -> mismatch, mismatches = 1, index = 3.
4. For height = 2: count[2] = 1, check heights[3] (2) -> match, index = 4.
5. For height = 3: count[3] = 1, check heights[4] (1) -> mismatch, mismatches = 2, index = 5.
6. For height = 4: count[4] = 1, check heights[5] (3) -> mismatch, mismatches = 3.
Final mismatches = 3.
Key Insights
- Sorting helps us find the expected order quickly.
- Counting occurrences can help us avoid sorting and improve efficiency.
Common Mistakes
- Not considering the range of heights when using arrays.
- Overlooking the need to compare the original and expected arrays correctly.
Interview Tips
- Explain your thought process clearly while coding.
- Consider edge cases, such as all students being of the same height.
- Practice explaining your solution to someone else to solidify your understanding.