关闭

LWC 69: 775. Global and Local Inversions

417人阅读 评论(0) 收藏 举报
分类:

LWC 69: 775. Global and Local Inversions

传送门:775. Global and Local Inversions

Problem:

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.

思路:
local inversion 很容易求解,遍历一遍O(n)结束,global inversion实际上就是求逆序对的个数。所以此题我们可以分别求解local inversion和global inversion之后再check。逆序对采用分治算法,详见http://blog.csdn.net/u014688145/article/details/79059221

Java版本:

    public boolean isIdealPermutation(int[] A) {
        int local = 0;
        int n = A.length;
        for (int i = 0; i < n; ++i) {
            if (i + 1 < n && A[i] > A[i + 1]) local ++;
        }
        count = 0;
        num = A;
        merge(0, n);
        return local == count;
    }

    int count = 0;
    int[] num;

    void merge(int s, int e) {
        if (e - s <= 1) return;
        int m = (s + e) / 2;
        merge(s, m);
        merge(m, e);
        for (int i = s, j = m; i < m; ++i) {
            while (j < e && num[i] > num[j]) j ++;
            count += j - m;
        }

        mergeSort(s, e);
    }

    void mergeSort(int s, int e) {
        int m = (s + e) / 2;
        int[] aux = new int[e - s];
        int i = s;
        int j = m;
        int k = 0;

        while (i < m && j < e) {
            if (num[i] < num[j]) aux[k++] = num[i++];
            else aux[k++] = num[j++];
        }

        while (i < m) aux[k++] = num[i++];
        while (j < e) aux[k++] = num[j++];

        for (int t = 0, l = s; l < e; ++l) num[l] = aux[t++];
    }

参考了几个脑洞大开的思路,比如
https://discuss.leetcode.com/topic/118695/check-if-we-can-sort-the-array-with-only-local-inversions提到:
1. 所有的local inversion 都是 global inversion。
2. 为了让local inversion == global inversion,让A中只存在local inversion即可。
3. A中只存在local inversion的充分必要条件是:对所有local inversion排序后得到A是有序的。

Python版本:

class Solution(object):
    def isIdealPermutation(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        n = len(A)
        for i in xrange(1, n):
            if A[i - 1] == A[i] + 1:
                A[i - 1], A[i] = A[i], A[i - 1]
            elif A[i - 1] != i - 1:
                return False
        return True

当然,你还可以这样:

The original order should be [0, 1, 2, 3, 4…], the number i should be on the position i. We just check the offset of each number, if the absolute value is larger than 1, means the number of global inversion must be bigger than local inversion, because a local inversion is a global inversion, but a global one may not be local.

Python版本:

class Solution(object):
    def isIdealPermutation(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        return all(abs(v - i) <= 1 for v, i in enumerate(A))
1
0
查看评论

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 ...
  • zjucor
  • zjucor
  • 2018-01-29 13:25
  • 140

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 A[j]. The numb...
  • liuchuo
  • liuchuo
  • 2018-01-29 15:24
  • 281

Learning with Local and Global Consistency

简介半监督学习问题(直推推断)的主要方法设计一个相对于intrinsic structure平滑的分类函数,本证结构由已标注和未标注的数据揭露。 给定一组数据X={x1,...,xl,xl+1,...,xn}\mathcal{X}=\{x_1,...,x_l,x_{l+1},...,x_n\}以及...
  • wendox
  • wendox
  • 2016-01-10 14:31
  • 950

*Leetcode 775. Global and Local Inversions

https://leetcode.com/contest/weekly-contest-69/problems/global-and-local-inversions/ 第一反应是暴力做法,O(nlogn))逆序数写出来bug了。需要重新写一下。 然后想了下可以O(n),只用修正local的...
  • u011026968
  • u011026968
  • 2018-02-05 21:14
  • 25

unity3d的local和global

local是自身坐标轴跟着转 global是不转的 pivot就是模型坐标轴的真实位置。这个是开发中常用的模式。 center是unity自己根据模型的mesh计算的中心位置,和模型真实坐标轴没关系了。
  • a351945755
  • a351945755
  • 2016-07-20 10:37
  • 1864

创建查看和浅析LOCAL INDEX和GLOBAL INDEX

 创建查看和浅析LOCAL INDEX和GLOBAL INDEX2008-09-16 15:45url:http://hi.baidu.com/klkl1110/blog/item/4c803fd99f80133332fa1c34.html1. 首先了解一下local index 和 gl...
  • jackpk
  • jackpk
  • 2009-01-15 13:13
  • 10031

Global 和 Local 索引。

1、创建表: create table immutable_local (id varchar not null primary key, cf1.a varchar, cf1.b varchar, cf2.c varchar, cf2.d varchar ) immutable_rows=tru...
  • lihui6636
  • lihui6636
  • 2014-11-12 08:57
  • 1339

unity3d中的local和global

这里有两个Cube,cube1是(1,1,1),Cube2是(6,6,6) 然后将Cube1拖成Cube2的父物体,Cube2就成了(5,5,5),以
  • u013291337
  • u013291337
  • 2015-08-01 15:36
  • 3877

操作分区表对global和local索引的影响操作分区表对global和local索引的影响

使用分区表,要非常注意索引。因为删除、move分区都会造成索引失效。小表的索引和非主键、唯一建索引还好,重建即可。如果是几十G的表,重建索引花费巨大,会严重影响查询性能; 如果是主键或者唯一键索引,索引失效会插入重复的数据,带来的麻烦更大。所以,我们一定要重视分区表操作,避免引起索引失效。 哪些操作...
  • rivarola
  • rivarola
  • 2014-12-29 10:41
  • 1186

Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions

题目要求如下: Download the text file here. (Right click and save link as) This file contains all of the 100,000 integers between 1 and 100,000 (inclu...
  • u012290414
  • u012290414
  • 2015-01-25 11:32
  • 792
    个人资料
    • 访问:216310次
    • 积分:5638
    • 等级:
    • 排名:第5548名
    • 原创:342篇
    • 转载:1篇
    • 译文:0篇
    • 评论:50条
    博客专栏
    最新评论