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
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
[Leetcode/C++] 1154. Day of the Year (0) | 2022.01.28 |
---|---|
[Leetcode/C++] 1047. Remove All Adjacent Duplicates In String (0) | 2022.01.26 |
[Leetcode/C++] 67. Add Binary (0) | 2022.01.24 |
[Leetcode/C++] 168. Excel Sheet Column Title (0) | 2022.01.24 |
[Leetcode/C++] 682. Baseball Game (0) | 2022.01.24 |