Leetcode 1051: Height Checker

grid47
grid47
Exploring patterns and algorithms
Jul 24, 2024 5 min read

A school is arranging students in a line for an annual photograph. The students are required to stand in a non-decreasing order of height. The expected order is represented by an array, where each element corresponds to the expected height of the student at that position. Given the current arrangement of the students’ heights, determine the number of positions where the height of the student does not match the expected height.
Problem
Approach
Steps
Complexity
Input: You are given an array 'heights' which represents the current arrangement of students, and another array 'expected' which represents the expected arrangement of students in non-decreasing order.
Example: Input: heights = [4, 2, 3, 5, 1], expected = [1, 2, 3, 4, 5]
Constraints:
• 1 <= heights.length <= 100
• 1 <= heights[i] <= 100
Output: Return the number of positions where the height in the 'heights' array does not match the corresponding value in the 'expected' array.
Example: Output: 3
Constraints:
• The result will be a non-negative integer.
Goal: The goal is to identify and count the indices where the current heights do not match the expected sorted order.
Steps:
• 1. Sort the 'heights' array to create the 'expected' array.
• 2. Compare the 'heights' array with the 'expected' array.
• 3. Count the number of indices where the two arrays differ.
Goal: You are guaranteed that the number of students will be between 1 and 100, and the heights are between 1 and 100.
Steps:
• 1 <= heights.length <= 100
• 1 <= heights[i] <= 100
Assumptions:
• The heights array contains positive integers that represent valid heights.
• The input array will not be empty.
Input: Input: heights = [4, 2, 3, 5, 1], expected = [1, 2, 3, 4, 5]
Explanation: In this example, after sorting, the heights array becomes [1, 2, 3, 4, 5]. The differences are at indices 0, 3, and 4, so the output is 3.

Input: Input: heights = [1, 2, 3, 4, 5], expected = [1, 2, 3, 4, 5]
Explanation: In this case, the 'heights' array is already in the expected order, so there are no differences. The output is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus