leetcode 775. Global and Local Inversions ---找规律

题解:

在global和local出现的情况相等时候,会发现,将local中出现逆序的情况反转,就能够得到一个升序排列的数组,

否则,如果swap两个逆序之后,不是升序的,会导致global的个数大于local的个数,如[1,2,0]中,2和0 交换后,不是升序

排列,除了2大于0,global的统计中1也大于0,因此glboal要比local的次数要多

bool isIdealPermutation(vector<int>& A) {

    if(A.size()<=1) return true;
    for(int i=1;i<A.size();i++)
    {
        if(A[i]==A[i-1]-1)
        {
            swap(A[i],A[i-1]);
        }
        else if((A[i]==A[i-1]+1)||(A[i]==A[i-1]+2))
        {
            continue;
        } else
        {
            return false;
        }

    }
    return true;
}

  

    def isIdealPermutation(self,A):
        """
        :type A: List[int]
        :rtype: bool
        """
        if len(A)<=1:
            return True
        for i in xrange(1,len(A),1):
            if A[i]==A[i-1]-1:
                tmp=A[i]
                A[i]=A[i-1]
                A[i-1]=A[i]
            elif A[i]==A[i-1]+1 or A[i]==A[i-1]+2:
                continue
            else:
                return False
        return True

  

原文地址:https://www.cnblogs.com/wuxiangli/p/8379877.html

时间: 2024-11-05 11:20:07

leetcode 775. Global and Local Inversions ---找规律的相关文章

775. 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 <

[LeetCode] 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 <

[Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions

We have some permutation Aof [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 <

【LeetCode】下一个排列【找规律】

实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. 以下是一些例子,输入位于左侧列,其相应输出位于右侧列. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/next-permutation 分析: 方法1:直接调

[leetcode-775-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 <

HDU 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

UVA - 1646 - Edge Case(找规律)

题意:n(3 <= n <= 10000)个结点组成一个圈,求匹配(即没有公共点的边集)的个数. 找规律为斐波那契的性质,因为数太大所以用的java大数. import java.math.BigInteger; import java.util.Scanner; public class Main{ public static int MAXN = 10000 + 10; public static BigInteger []c = new BigInteger[MAXN]; public

【55测试】【字符串】【栈】【找规律】

集训第二天,额,考崩了. 第一题 hao 大意:(这个名字就不要在意了,其实是祖玛游戏) 模拟祖玛游戏的模式,给一个'A'~'Z'的字符串,然后有t个插入操作为 “ 添加后的在原字符串的位置 x  插入元素 c ”,字符串中有超过或等于 3 个相同的字符,则被消除,输出每次操作后剩余的字符串,如果为空,则输出“-”. 样例: ACCBA                     输出:  ABCCBA 5   AABCCBA 1 B AABBCCBA 0 A - 2 B A 4 C 0 A 解: