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.
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).
Return the number of indices where heights[i] != expected[i]
.
Example 1:
Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1,4,2,1,3] expected: [1,1,1,2,3,4] Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [5,1,2,3,4] expected: [1,2,3,4,5] All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5] Output: 0 Explanation: heights: [1,2,3,4,5] expected: [1,2,3,4,5] All indices match.
Constraints:
1 <= heights.length <= 100
1 <= heights[i] <= 100
program main
implicit none
integer, parameter :: n = 6
integer, dimension(n) :: heights, expected
integer :: i, j, count
! Examples
heights = [1, 1, 4, 2, 1, 3]
write (*, '(A)') 'Example 1:'
call solve(heights, expected)
write (*, '(A)') 'Expected: 3'
write (*, '(A)') 'Actual: ', count
heights = [5, 1, 2, 3, 4]
write (*, '(A)') 'Example 2:'
call solve(heights, expected)
write (*, '(A)') 'Expected: 5'
write (*, '(A)') 'Actual: ', count
heights = [1, 2, 3, 4, 5]
write (*, '(A)') 'Example 3:'
call solve(heights, expected)
write (*, '(A)') 'Expected: 0'
write (*, '(A)') 'Actual: ', count
contains
subroutine solve(heights, expected)
implicit none
integer, intent(in) :: heights(:), expected(:)
integer :: i, j
count = 0
do i = 1, size(heights) - 1
if (heights(i) /= expected(i)) then
count = count + 1
end if
end do
end subroutine solve
end program main
temp.f95:15:4: 15 | heights = [5, 1, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 5) temp.f95:21:4: 21 | heights = [1, 2, 3, 4, 5] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 5)
module main
implicit none
integer, parameter :: int64 = selected_int_kind(13)
interface
function solve(heights, expected) result(res)
integer(int64), dimension(:), intent(in) :: heights
integer(int64), dimension(:), intent(in) :: expected
integer(int64) :: res
end function solve
end interface
contains
subroutine run_test_case(heights, expected, output)
integer(int64), dimension(:), intent(in) :: heights
integer(int64), dimension(:), intent(in) :: expected
integer(int64), intent(out) :: output
output = solve(heights, expected)
write (*,*) "Output: ", output
write (*,*) "Expected: ", expected
write (*,*) "Pass: ", (output == expected)
write (*,*) ""
end subroutine run_test_case
subroutine run_test_cases()
integer(int64), dimension(5) :: heights
integer(int64), dimension(5) :: expected
integer(int64) :: output
! Test case 1:
heights = [1, 1, 4, 2, 1, 3]
expected = [1, 1, 1, 2, 3, 4]
call run_test_case(heights, expected, output)
! Test case 2:
heights = [5, 1, 2, 3, 4]
expected = [1, 2, 3, 4, 5]
call run_test_case(heights, expected, output)
! Test case 3:
heights = [1, 2, 3, 4, 5]
expected = [1, 2, 3, 4, 5]
call run_test_case(heights, expected, output)
end subroutine run_test_cases
end module main
module solution
contains
function solve(heights, expected) result(res)
integer(int64), dimension(:), intent(in) :: heights
integer(int64), dimension(:), intent(in) :: expected
integer(int64) :: res
res = count(heights /= expected)
end function solve
end module solution
program main
use solution
implicit none
integer(int64), dimension(5) :: heights
integer(int64), dimension(5) :: expected
integer(int64) :: output
call run_test_cases()
end program main
temp.f95:10:12: 10 | integer(int64), dimension(:), intent(in) :: heights | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:11:12: 11 | integer(int64), dimension(:), intent(in) :: expected | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:12:12: 12 | integer(int64) :: res | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:39:4: 39 | heights = [1, 1, 4, 2, 1, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6) temp.f95:40:4: 40 | expected = [1, 1, 1, 2, 3, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6) temp.f95:24:13: 24 | output = solve(heights, expected) | 1 Error: Type mismatch in argument ‘heights’ at (1); passed INTEGER(8) to REAL(4) temp.f95:62:12: 62 | integer(int64), dimension(:), intent(in) :: heights | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:63:12: 63 | integer(int64), dimension(:), intent(in) :: expected | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:64:12: 64 | integer(int64) :: res | 1 Error: Parameter ‘int64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression temp.f95:66:16: 66 | res = count(heights /= expected) | 1 Error: ‘mask’ argument of ‘count’ intrinsic at (1) must be a logical array temp.f95:72:12: 1 | module main | 2 ...... 72 | program main | 1 Error: Global name ‘main’ at (1) is already being used as a MODULE at (2) temp.f95:73:9: 73 | use solution | 1 Fatal Error: Cannot open module file ‘solution.mod’ for reading at (1): No such file or directory compilation terminated.
def height_checker(heights):
expected = sorted(heights)
count = 0
for i in range(len(heights)):
if heights[i] != expected[i]:
count += 1
return count
#include <vector>
#include <algorithm>
int heightChecker(std::vector<int>& heights) {
std::vector<int> expected = heights;
std::sort(expected.begin(), expected.end());
int count = 0;
for (int i = 0; i < heights.size(); ++i) {
if (heights[i] != expected[i]) {
++count;
}
}
return count;
}