【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- mac打开已损坏或者不明来源的应用的解决方法
如遇:「xxx.app已损坏,打不开.你应该将它移到废纸篓」,并非你安装的软件已损坏,而是Mac系统的安全设置问题,因为这些应用都是破解或者汉化的,那么解决方法就是临时改变Mac系统安全设置.出现这个 ...
- c++面向对象 —— 类和对象
类和对象 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法.类中的数据和方法称为类的成员.函数在一个类中被称为类的成员. 一.类定义 实际上并没有定义任何数据,但它定义了类的名称意味着什么 ...
- Unity2D RPG游戏开发日志
一.游戏构建设计 场景设计:地面的每一层用unity的TiledMap来设计,首先第一层为地面层,也就是地形的大部分区域的图块:第二层为覆盖层,如图中蓝色线圈起来的柱子的上半部分,由于玩家可以在柱子背 ...
- cocos2dx[3.2](11) 新事件分发机制
在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐. 而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器Eve ...
- 【机器学习】聚类算法:ISODATA算法
在之前的K-Means算法中,有两大缺陷: (1)K值是事先选好的固定的值 (2)随机种子选取可能对结果有影响 针对缺陷(2),我们提出了K-Means++算法,它使得随机种子 ...
- Java基础/网络经验
一.Java新特性好文--掘金 1.Java8 新特性指导手册 2.Java 11 已发布,String 还能这样玩 二.Java避坑 1.为什么阿里巴巴不建议在for循环中使用"+&quo ...
- mysql——多表——内连接查询
内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表: 当该字段的值相等时,就查询出该记录. 前期准备两个表: ), d_id ), name ) ...
- python logger 日志模块
logger 日志 """logging配置""" import osimport logging.config # 定义三种日志输出格式 ...
- shiro登陆认证
1.LoginController @RequestMapping(method = RequestMethod.POST) public String login(User user, HttpSe ...
- HDU 1042 N!(高精度阶乘、大数乘法)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...