본문 바로가기

Problem Solving/리트코드(leetcode)

[Leetcode/C++] 1051. Height Checker


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

왜 문제가 height checker인지 모르겠다. 뒤에 숫자가 작으면 안보이는 그런 문제를 생각했는데 그냥 sorting 되지 않은 것이 몇개인가 출력하는 문제였다.

 

같은 배열을 assign하여 받아오고 sort한 뒤 몇개가 다른지 센 후 출력해주었다.


문제: https://leetcode.com/problems/height-checker/

 

Height Checker - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com