【LeetCode】775. Global and Local Inversions 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/global-and-local-inversions/description/
题目描述:
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.
题目大意
如果存在i < j with 0 <= i < j < N and A[i] > A[j],称之为一个全局翻转。
如果存在0 <= i < N and A[i] > A[i+1],称之为一个局部翻转。
判断一个由0~N - 1组成的一个乱序数组中,全局翻转的个数与局部翻转的个数是否相等。
解题方法
首先当j = i + 1时,可以看出,一个局部翻转就是一个全局翻转。那么如果要使得局部翻转和全局翻转的个数相等,那么必须要求全局翻转也是一个局部翻转。所以,对于任意的j > i + 1,不能存在A[i] > A[j],即需要满足A[i] <= A[j].
从上面的关系可以看出,我们必须使max(A[:i]) <= A[i + 2]。
最坏情况下的时间复杂度是O(N),空间复杂度是O(1)。
class Solution(object):
def isIdealPermutation(self, A):
"""
:type A: List[int]
:rtype: bool
"""
cmax = 0
for i in range(len(A) - 2):
cmax = max(cmax, A[i])
if cmax > A[i + 2]:
return False
return True
上面的想法并没有好好的利用题目给出的数字是0N-1这个条件。所以我们继续思考,如果原来的顺序是0N-1,那么如何交换两个数字才能满足局部翻转的个数等于全局翻转呢?答案当然是只翻转相邻的两个元素。否则会构造出来一个不是局部翻转的全剧翻转。所以i的位置上只能放A[i-1],A[i],A[i+1]。
class Solution(object):
def isIdealPermutation(self, A):
"""
:type A: List[int]
:rtype: bool
"""
for i, a in enumerate(A):
if abs(a - i) > 1:
return False
return True
参考资料:
日期
2018 年 10 月 1 日 —— 欢度国庆!
【LeetCode】775. Global and Local Inversions 解题报告(Python)的更多相关文章
- 【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 (g ...
- 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) ...
- 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 (g ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
随机推荐
- ubuntu终端颜色快速配置
ubuntu终端颜色快速配置 根据以下step步骤设置即可 step1:备份:cp ~/.bashrc ~/.bashrc.backup step2:打开文件:vim ~/.bashrc step3: ...
- day15 数组
day15 数组 数组 1.什么是数组? 什么是数组? 具备某种相同属性的数据集合 [root@localhost ~]# array_name=(ddd) [root@localhost ~]# d ...
- Hbase(一)【入门安装及高可用】
目录 一.Zookeeper正常部署 二.Hadoop正常部署 三.Hbase部署 1.下载 2.解压 3.相关配置 4.分发文件 5.启动.关闭 6.验证 四.HMaster的高可用 一.Zooke ...
- 【分布式】Zookeeper的Leader选举-选举过程介绍(经典的Paxos算法解析)
一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...
- Linux学习 - 环境变量配置文件
一.环境变量配置文件的作用 /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~/.bashrc /etc/bashrc 1 /etc/profile的 ...
- 视图View,获取视图大小
一.获得LayoutInflater实例: LayoutInflater layoutInflater=LayoutInflater.from(context); 得到LayoutInflater实例 ...
- java内存管理的小技巧
1,尽量使用直接量. 采用String str="hello"; 而不是 String str = new String("hello"): 2,使用S ...
- 最小化安装centos ubuntu基础命令
# yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \ pcre-devel openssl openssl-devel zip ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(8. 安装和配置Kubernetes)
1. 概念 1.1. 比较主流的任务编排系统有mesos+marathon,swarm,openshift(红帽内部叫atom服务器)和最著名的kubernetes,居然说yarn也行,不过没见过谁用 ...
- Ajax异步更新网页(使用原生JavaScript)
一.页面代码 <!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <me ...