[
sort
array
]
BinarySearch 0153 Sorted Elements
Problem statement
https://binarysearch.com/problems/Sorted-Elements/
Solution
Just sort and check places.
Complexity
It is O(n log n) for time and O(n) for space.
Code
class Solution:
def solve(self, nums):
return sum(x == y for x, y in zip(nums, sorted(nums)))
Remark
See Leetcode 0581. Shortest Unsorted Continuous Subarray problem, where we have also O(n) time complexity solution.