#775

Global and Local Inversions

specialist · 990 · lc medium +32 · verified · 42.7% accepted · 1,875 likes · top 24%

Description

Given a length-n permutation nums of [0, n-1], a global inversion is any pair (i, j) with i < j and nums[i] > nums[j], while a local inversion is any adjacent pair (i, i+1) with nums[i] > nums[i+1]. Every local inversion is also a global inversion.

Return true if the counts of global and local inversions are equal (i.e., there are no non-adjacent global inversions).

Example 1:

Input: nums = [1,0,2]
Output: true
Explanation: There is 1 global inversion and 1 local inversion.

Example 2:

Input: nums = [1,2,0]
Output: false
Explanation: There are 2 global inversions and 1 local inversion.

Code

1
2
3