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 <= heights.length <= 1001 <= heights[i] <= 100题目大意:
先总结一下138周赛的题目,这一周的题目相对简单,有一些技巧性的题目但是也很容易想到解决方案,好像AK也只能排到600多名,世道艰难。
本题给出一个数组,我们需要是将这个序列排列成一个非递减的序列,问最小有几个人排错。
解题思路:
先排序,对应位置找出不同的数,记录个数,输出。
class Solution { public: int heightChecker(vector<int>& h) { int n = h.size(); int ans = 0; vector<int> tmp(h.begin(), h.end()); sort(h.begin(), h.end()); for(int i=0;i<n;i++){ if(tmp[i]!=h[i]){ ans++; } } return ans; } };