描述
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 <= 100
1 <= heights[i] <= 100
解析
其实就是先把站位顺序从小到大排列一下,然后和原数组比较有多少个位置的元素不一样就可以了,时间复杂度是 O(NlogN)。
解答
class Solution(object):
def heightChecker(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
return sum(x!=y for x,y in zip(sorted(heights),heights))
运行结果
Runtime: 16 ms, faster than 80.23% of Python online submissions for Height Checker.
Memory Usage: 11.7 MB, less than 100.00% of Python online submissions for Height Checker.
每日格言:与魔鬼战斗的人,应当小心自己不要成为魔鬼。当你远远凝视深渊时,深渊也在凝视你。
感谢支持 支付宝