LeetCode Brush Questions 1051. Height Checker

LeetCode Brush Questions 1051. Height Checker

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic :
    Schools in the annual commemorative photo shoot, usually require students to follow non-decreasing arrangement order of height. Please return at least the number of students who are not standing in the correct position. The number refers to: allow all students to non-decreasing number of people necessary to move the highly ranked.
  • Example :
示例 1 :
输入:[1,1,4,2,1,3]
输出:3
解释:高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。
  • Tips :
    1. 1 <= heights.length <= 100
    2. 1. 1 <= heights[i] <= 100
  • Code:
class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        a = heights.copy()
        a.sort()
        count = 0
        for i in range(len(heights)):
            if a[i] != heights[i]:
                count += 1
        return(count)
# 执行用时 :64 ms, 在所有 Python3 提交中击败了27.09%的用户
# 内存消耗 :13.8 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description: The
    meaning of the title is to find the number of different elements in the list after sorting in ascending order. First assign the elements of the list to a, and then arrange them ain ascending order, compare the number of different elements in the two lists one by one, and return.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/106800341