leetcode 1051. Height Checker

Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students not standing in the right positions.  (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)

 

Example 1:

Input: [1,1,4,2,1,3]
Output: 3
Explanation: 
Students with heights 4, 3 and the last 1 are not standing in the right positions.

 

Note:

  1. 1 <= heights.length <= 100
  2. 1 <= heights[i] <= 100

简单题。

 1 class Solution {
 2 public:
 3     int heightChecker(vector<int>& heights) {
 4         vector<int> ret(heights);
 5         int wrongNum = 0;
 6         sort(ret.begin(), ret.end());
 7         for (int i = 0; i < heights.size(); ++i) {
 8             if (heights[i] != ret[i])
 9                 ++wrongNum;
10         }
11         return wrongNum;
12     }
13 };

 

posted @ 2019-11-14 20:04  琴影  阅读(349)  评论(0编辑  收藏  举报