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 < 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.
思路:
Key insights:
- every local inversion is also a global inversion
- so “local inversions == global inversions” can be interpreted as “there are only local inversions”
- if there are only local inversions, the array will be sorted after making all local inversions
- if there are inversions that are not local, the array won’t be sorted after making all local inversions
class Solution {
public boolean isIdealPermutation(int[] A) {
for(int i = 0;i < A.length-1;){
if(A[i]>A[i+1]){
int temp = A[i];
A[i] =A[i+1];
A[i+1] = temp;
i += 2;
}else{
i++;
}
}
for(int i = 0;i < A.length -1; i++){
if(A[i]>A[i+1]){
return false;
}
}
return true;
}
} public class Main {
public static void main(String[] args){
Solution solution = new Solution();
int[] A = {1,2,0};
solution.isIdealPermutation(A);
}
}
775. Global and Local Inversions的更多相关文章
- 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】775. Global and Local Inversions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/global-a ...
- [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions
We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...
- [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) ...
- 【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 ...
- [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) ...
- oracle11.2中分区功能测试之add&split partition对global&local index的影响
生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测 ...
- 侧脸生成正脸概论与精析(一)Global and Local Perception GAN
侧脸生成正脸我一直很感兴趣,老早就想把这块理一理的.今天来给大家分享一篇去年的老文章,如果有不对的地方,请斧正. Beyond Face Rotation: Global and Local Perc ...
- 论文笔记:Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language Association
Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language ...
随机推荐
- liunx 修改ssh 端口22
vim /etc/ssh/sshd_config 找到Port 22 添加 Port 5002 重启sshd /bin/systemctl restart sshd.service 防火墙释放5 ...
- hash与encrypt
概括来说,哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可逆的密文. 具体来说,两者有如下重要区别: ...
- 当linux报 “-bash: fork: 无法分配内存”
“-bash: fork: 无法分配内存”,发现连了好多终端,然后断开了一个终端,然后这边终端可以敲命令了 [root@172.16.31.105 /home/www/test]# free -m ...
- Orleans框架简单示范
希望现在学习Orleans不会晚,毕竟Server Fabric都开源了.本篇博客从Sample的HelloWorld示例程序来解读Orleans的Grains. Server配置参考 : https ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- Django实例
更新:今年8月在深圳和嵩天老师居然见面了,很开心.嵩天老师很和蔼. =========== 今天看了嵩天老师的视频,感觉讲的很好,于是看着视频自己做了一个初步的实例认识. 步骤1,新建一个Web框架 ...
- 管理Android设备的唤醒状态
当一个Android设备闲置时,首先它的屏幕将会变暗,然后关闭屏幕,最后关闭CPU. 这样可以防止设备的电量被迅速消耗殆尽.但是,有时候也会存在一些特例: Apps such as games or ...
- php curl 跨域情趣
function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ch,CURL ...
- Linux 安装zookeeper
分享到: 1.下载zokeeper 1.1 官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 1.2 链接:https:/ ...
- 51nod 1387 移数字
任意门 回来拉模版的时候意外发现这个题还没写题解,所以就随便补点吧. 题意其实就是要你求n的阶乘在模意义下的值. 首先找出来一个最大的$m$满足$m^2<=n$,对于大于$m^2$部分的数我们直 ...