0% completed
A school is organizing its annual photo session, and students must stand in a single line arranged by height in non-decreasing order. The expected order of heights is represented by an array expected
, where expected[i]
is the height of the i<sup>th</sup> student in line.
Given an array heights
representing the current order in which students are standing, determine how many positions in the heights
array do not match the expected
order.
Example 1:
[5, 1, 2, 3, 4, 8, 1]
3
[1, 1, 2, 3, 4, 5, 8]
. Comparing with heights
, the positions that do not match are 0, 5 and 6.Example 2:
[10, 6, 8, 5, 9]
3
[5, 6, 8, 9, 10]
. Comparing with heights
, the positions that do not match are 0, 3 and 4.Example 3:
[7, 3, 2, 1, 4]
5
[1, 2, 3, 4, 7]
. All positions differ from heights
.Constraints:
1 <= heights.length <= 100
1 <= heights[i] <= 100
To solve this problem, we use a counting sort approach to determine how many students are standing in incorrect positions. We start by creating a frequency array to count the occurrences of each height in the heights
array. This frequency array helps us understand how many times each height appears, making it easy to reconstruct the sorted order of heights. By leveraging the count of each height, we can then build the expected sorted array without actually sorting the original array, which is more efficient.
Next, we compare the original heights
array with the reconstructed sorted array. We iterate through the heights
array and, for each position, check if the height matches the expected height from the sorted array. If there is a mismatch, we increment a counter. This approach works efficiently because it avoids the need for a traditional sorting algorithm, instead relying on the counting sort's linear time complexity to handle the sorting implicitly.
Initialize Variables:
max_height
as the maximum value in the heights array (given as 100 for simplicity).count
of size max_height + 1
initialized to zero.Count Frequencies:
heights
array.count
array.Initialize Result and Current Height:
result
to zero, which will count the mismatches.currentHeight
to zero, which will track the current height being processed.Compare Arrays:
heights
array using an index i
.heights
, find the next non-zero count in the count
array:
height[i]
.count[currentHeight]
is zero, increment currentHeight
. In this step, we skip index related to elements which are not in the heights
array.heights[i]
with currentHeight
:
result
.currentHeight
in the count
array.Return Result:
result
as the number of positions where the heights do not match the expected order.Given heights = [5, 1, 2, 3, 4, 8, 1]
:
Initialize Variables:
max_height = 100
.count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(array of size max_height + 1
).Count Frequencies:
heights
:
count[5]
becomes 1.count[1]
becomes 1.count[2]
becomes 1.count[3]
becomes 1.count[4]
becomes 1.count[8]
becomes 1.count[1]
becomes 2.count
array: [0, 2, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
.Initialize Result and Current Height:
result = 0
.currentHeight = 0
.Compare Arrays:
heights
with index i
:
i = 0
, heights[0] = 5
:
count[currentHeight] == 0
, increment currentHeight
to 1.result
to 1.count[1]
to 1.i = 1
, heights[1] = 1
:
result
remains 1.count[1]
to 0.i = 2
, heights[2] = 2
:
currentHeight
to 2 (since count[1]
is 0).result
remains 1.count[2]
to 0.i = 3
, heights[3] = 3
:
currentHeight
to 3 (since count[2]
is 0).result
remains 1.count[3]
to 0.i = 4
, heights[4] = 4
:
currentHeight
to 4 (since count[3]
is 0).result
remains 1.count[4]
to 0.i = 5
, heights[5] = 8
:
currentHeight
to 5 (since count[4]
is 0).result
to 2.count[5]
to 0.i = 6
, heights[6] = 1
:
currentHeight
to 8 (since count[5]
to count[7]
is 0).result
to 3.count[8]
to 0.Return Result:
result
, which is 3.heights
array once to populate the count
array, which takes O(n) time.heights
array again to compare each height with the expected order, which also takes O(n) time. The inner while loop that increments currentHeight
only iterates a constant number of times (at most 100 times, due to the constraints), which does not add significant complexity.count
array size is constant (fixed at 101), regardless of the input size due to given constraints. We do not use any additional space that scales with the input size......
.....
.....