【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 < 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.
解题思路:这是一个非常有趣的题目。我最初的想法是用一个二层的循环计算出Global的值,然后好Local比较,但是O(n^2)的复杂度显然是不行的,然后我各种优化但是得到的依旧是冷冰冰的Time Exceed Limit.考虑到题目只是要求判断Global是否会等于Local,机智的我果断放弃了计算出Global的值的尝试,开始寻找更巧妙的解法。皇天不负有心人,果然被我找到了。对应任意一个数字A[i]来说,它的Global一定是大于或者等于Local的,也就是说,在整个A中,只要找到任意一个A[i]满足在A[i+2]~A[N-1]里面有一个小于A[i]的数,最终整个数组的Global就一定会大于Local了。为什么不用考虑A[i+1]?无论A[i]和A[i+1]的大小关系如何,Global和Local的值要不分别加1,要么不变,所以不管。那么问题来了,怎么找到任意一个A[i]满足在A[i+2]~A[N-1]里面有一个小于A[i]的数呢?只需要依次遍历数组,找出以遍历元素中的最大值(记为max)和当前遍历到的元素的后面第二个元素(记为A[i+2])比较就行了,如果max > A[i+2] ,那么Global就一定会大于Local了。
代码如下:
class Solution(object):
def isIdealPermutation(self, A):
"""
:type A: List[int]
:rtype: bool
"""
maxV = -1
for i in range(len(A)-2):
if maxV == -1 or maxV < A[i]:
maxV = A[i]
if maxV > A[i+2]:
return False
return True
【leetcode】Global and Local Inversions的更多相关文章
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- unity中的常遇到的问题
1.使用unity的MovieTexture播放视频在物体上,对象只能在电脑上 2.移动端播放全屏视频 Handheld.PlayFullScreenMovie(),视频文件必须放置在Streamin ...
- 《Google工作法》读书笔记
最近一段时间,拜读了<Google工作法>,工作效率提升10倍的57个技巧. 作者是彼得·费利克斯·格日瓦奇,波兰人. 其中印象最深刻的部分如下: (1)不要被邮件夺走时间 不用邮件,所有 ...
- Linux执行命令./command与直接输入命令的区别
我们知道查看文件属性的命令ls的完整文件路径为:/bin/ls(这是绝对路径,)问什么我可以在任何地方执行,任何目录下输入ls就一定可以显示出一些信息而不会说找不到该/bin/ls命令,这是因为不同的 ...
- 20191224 Spring官方文档(启动)
再学Spring 之前看过Spring教学视频,看过<Spring5高级编程>,但是对于Spring始终还是感觉差了一点,应该是底层没有学好,这次再学Spring,就是要将Spring底层 ...
- mysql——触发器——前期整理笔记00
一.触发器 触发器是由事件来出发某个动作.这些事件包括insert语句.update语句和delete语句. 当数据库系统执行这些事件时,就会激活触发器执行相应得动作. 触发器是有insert.upd ...
- 如何在CBV中使用装饰器
要区分函数装饰器和方法装饰器得区别 ,方法装饰器得第一个参数是self本身,所以函数装饰器不能用
- python 序列 转换 各种操作
# 数据结构 字符串 列表 元组 数字序列# 10-19的整数# r1 = range(10,20)# print(r1)# print(type(r1))## # 19# print(r1[9])# ...
- HDU-4717 The Moving Points(凸函数求极值)
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- limux密钥对配置登陆主机
1. Linux主机免密码使用密钥登陆 这里假设主机A(192.168.0.113)用来远程连接主机B(192.168.0.186) 在主机A上执行如下命令来生成配对密钥:ssh-keygen -t ...
- c#服务端图片打包下载
一,设计多图片打包下载逻辑:1,如果是要拉取腾讯云等资源服务器的图片,2,我们先把远程图片拉取到本地的临时文件夹,3,然后压缩临时文件夹,4,压缩完删除临时文件夹,5,返回压缩完给用户,6,用户就去请 ...