https://leetcode.com/problems/height-checker
1051. Height Checker
给定一数列heights 我们期待heights是一非递减数列
此理想数列设为expected
请回传heights[i] != expected[i]的数量
Example 1:
Input: heights = [1,1,4,2,1,3]
Output: 3
Explanation:
heights: [1,1,4,2,1,3]
expected: [1,1,1,2,3,4]
Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4]
Output: 5
Explanation:
heights: [5,1,2,3,4]
expected: [1,2,3,4,5]
All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5]
Output: 0
Explanation:
heights: [1,2,3,4,5]
expected: [1,2,3,4,5]
All indices match.
Constraints:
1 <= heights.length <= 100
1 <= heights[i] <= 100
思路:
排序
Python Code:
class Solution:
def heightChecker(self, heights: List[int]) -> int:
n = sorted(heights)
result = 0
for i in range(len(n)):
if n[i] != heights[i]:
result += 1
return result
看解答好像能把时间复杂度降到n 等等研究一下