LeetCode solutions

1137. Height Checker

My accepted C++ solution to LeetCode problem 1137, Height Checker, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 11.5MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 11.5MB, accepted 2025-12-27.

cpp
class Solution {
public:
    int heightChecker(vector<int>& heights) {
        vector<int> expected = heights;
        sort(expected.begin(), expected.end());
        int count = 0;
        for (int i = 0; i < heights.size(); i++) {
            if (heights[i] != expected[i]) count++;
        }
        return count;
    }
};

Source