【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 ...
随机推荐
- zabbix 内网机器通信状态
a=0 for xgip in ${xgipset[*]} do let a+=1 fping $xgip|grep alive >/dev/null if [ $a != 3 ];then i ...
- Excel-实现选择性粘贴(粘贴公式为文本)自动化,不想手动
10.选择性粘贴(粘贴公式为文本)自动化,不想手动: (1)参考:https://jingyan.baidu.com/article/20b68a88a8bf55796cec62a3.html (2) ...
- QQ空间技术架构之深刻揭秘
QQ空间技术架构之深刻揭秘 来源: 腾讯大讲堂 发布时间: 2012-05-17 17:24 阅读: 7822 次 推荐: 4 [收藏] QQ 空间作为腾讯海量互联网服务产品,经过近七年 ...
- SPI详解2
串行外设接口 (SPI) 总线是一种运行于全双工模式下的同步串行数据链路.用于在单个主节点和一个或多个从节点之间交换数据. SPI 总线实施简单,仅使用四条数据信号线和控制信号线(请参见图 1). 图 ...
- WebRTC视频分辨率设置
前面我们能够打开摄像头.getUserMedia()时会传入参数,在参数里我们可以指定宽高信息.通过宽高参数控制输出的视频分辨率. html 在页面上摆放一些元素,下面是主要部分 <div id ...
- LeetCode替换空格
LeetCode 替换空格 题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 实例 1: 输入:s = "We are happy." 输 ...
- Spark On Yarn的各种Bug
今天将代码以Spark On Yarn Cluster的方式提交,遇到了很多很多问题.特地记录一下. 代码通过--master yarn-client提交是没有问题的,但是通过--master yar ...
- HTTP请求 Java API
1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...
- JavaEE复习二
Servlet应用开发接口: javax.servlet.Servlet: init()方法:调用在构造方法之后,在service方法之前: service()方法:调用此方法允许Servlet响应请 ...
- 关于python中显存回收的问题
技术背景 笔者在执行一个Jax的任务中,又发现了一个奇怪的问题,就是明明只分配了很小的矩阵空间,但是在多次的任务执行之后,显存突然就爆了.而且此时已经按照Jax的官方说明配置了XLA_PYTHON_C ...