LeetCode 775 Global and Local Inversions

本文探讨了如何高效地解决LeetCode题目《全球与局部逆序》。介绍了从基本解决方案的O(n^2)复杂度到优化方案,利用输入特性简化为O(n)的时间复杂度。核心在于理解局部和全局逆序的定义,并通过巧妙的遍历策略避免双层循环。
摘要由CSDN通过智能技术生成

775 Global and Local Inversions

https://leetcode.com/problems/global-and-local-inversions/

We have some permutation A of [0, 1, ..., N - 1], where N is the length of A.

The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j].

The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1].

Return true if and only if the number of global inversions is equal to the number of local inversions.

Example 1:

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

Example 2:

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

Note:

  • A will be a permutation of [0, 1, ..., A.length - 1].
  • A will have length in range [1, 5000].
  • The time limit for this problem has been reduced.

Basic Solution

按题目定义计算 #local inversions #global inversions

#local inversions 很好计算,遍历一次判断前一位比后一位大即可,时间复杂度 O(n)

#global inversions 需要双重循环,时间复杂度 O(n^2),运行超时

class Solution {
public:
    bool isIdealPermutation(vector<int>& A) {
        int n = A.size();
        //local inversion
        int l = 0;
        for(int i = 0; i < n-1; ++i){
            if(A[i] > A[i+1])   ++l;
        }
        //global inversion
        int g = 0;
        for(int i = 0; i < n-1; ++i){
            for(int j = i+1; j < n; ++j){
                if(A[i] > A[j])  ++g;
            }
        }
        return l == g;
    }
};

进行优化,不直接计算 #global inversions ,只要在遍历时发现满足 A[i] > A[j] 并且此时 global + 1 后大于前面算出的 #local inversions ,就直接返回失败。

虽然理论上速度有所提升,但时间复杂度还是 O(n^2),仍然运行超时。

class Solution {
public:
    bool isIdealPermutation(vector<int>& A) {
        int n = A.size();
        //local inversion
        int l = 0;
        for(int i = 0; i < n-1; ++i){
            if(A[i] > A[i+1])   ++l;
        }
        //global inversion
        int g = 0;
        for(int i = 0; i < n; ++i){
            for(int j = i+1; j < n; ++j){
                if(A[i] > A[j] && ++g > l)  return false;
            }
        }
        return l == g;
    }
};

Input Property Solution

分析 local inversionsglobal inversions 的含义

  • local inversions 是相邻的两位中前一位比后一位大
  • local inversions一定属于 global inversions ,也就是 #global inversions >= #local inversions
  • 如果需要二者相等,就相当于面对一个顺序排列,进行的乱序必须符合,调换只在相邻的两位中进行;否则一次调换引起的 global inversions 必定超过 1,最终 #global inversions > #local inversions

因此我们判定的条件是,只要任意一个位置上的数字与下标之差超过 1,就代表最终的结果不等,返回 false

时间复杂度 O(n),空间复杂度 O(1)

class Solution {
public:
    bool isIdealPermutation(vector<int>& A) {
        for(int i = 0; i < A.size(); ++i){
            if(abs(A[i] - i) > 1)   return false;
        }
        return true;
    }
};

Runtime: 40 ms, faster than 92.41% of C++ online submissions forGlobal and Local Inversions.

Memory Usage: 35.7 MB, less than 52.90% of C++ online submissions for Global and Local Inversions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值