Skip to content

Commit 7debd6c

Browse files
committed
new soln
1 parent dce3fca commit 7debd6c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

1051.HeightChecker.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 1051. Height Checker
2+
// A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
3+
// You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).
4+
// Return the number of indices where heights[i] != expected[i].
5+
6+
// Example 1:
7+
// Input: heights = [1,1,4,2,1,3]
8+
// Output: 3
9+
// Explanation:
10+
// heights: [1,1,4,2,1,3]
11+
// expected: [1,1,1,2,3,4]
12+
// Indices 2, 4, and 5 do not match.
13+
// Example 2:
14+
// Input: heights = [5,1,2,3,4]
15+
// Output: 5
16+
// Explanation:
17+
// heights: [5,1,2,3,4]
18+
// expected: [1,2,3,4,5]
19+
// All indices do not match.
20+
// Example 3:
21+
// Input: heights = [1,2,3,4,5]
22+
// Output: 0
23+
// Explanation:
24+
// heights: [1,2,3,4,5]
25+
// expected: [1,2,3,4,5]
26+
// All indices match.
27+
28+
// Constraints:
29+
// 1 <= heights.length <= 100
30+
// 1 <= heights[i] <= 100
31+
32+
public class Solution {
33+
public int HeightChecker(int[] heights) {
34+
int[] expected = (int[])heights.Clone();
35+
Array.Sort(expected);
36+
int count = 0;
37+
for (int i = 0; i < heights.Length; i++) {
38+
if (heights[i] != expected[i])
39+
count++;
40+
}
41+
return count;
42+
}
43+
}

0 commit comments

Comments
 (0)