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 ...
随机推荐
- python学习之Numpy.genfromtxt
Python 并没有提供数组功能,虽然列表 (list) 可以完成基本的数组功能,但它并不是真正的数组,而且在数据量较大时,使用列表的速度就会慢的让人难受.Numpy 提供了真正的数组功能,以及对数据 ...
- java控台输入
import java.util.Scanner;//访问util包的Scanner(控台输入) public class HelloWorld {public static void main(St ...
- 【转】【数据结构】【有n个元素依次进栈,则出栈序列有多少种】
卡特兰数 大神解释:https://blog.csdn.net/akenseren/article/details/82149145 权侵删 原题 有一个容量足够大的栈,n个元素以一定的顺序 ...
- 001.Ansible部署RHCS存储集群
一 前期准备 1.1 前置条件 至少有三个不同的主机运行monitor (MON)节点: 至少三个直接存储(非外部SAN硬件)的OSD节点主: 至少两个不同的manager (MGR)节点: 如果使用 ...
- 【C#】多数组间的取重取余
string[] arrRate = new string[] { "a", "b", "c", "d" };//A s ...
- Chapter_9 DP : uva1347 tour (bitonic tour)
https://cn.vjudge.net/problem/UVA-1347 这道题居然可以O(n^2)解决, 让我太吃惊了!!! 鄙人见识浅薄, 这其实是一个经典问题: bitonic tour. ...
- 英语口语练习系列-C39-舞蹈-谈论昨天的活动
词汇-舞蹈(dancing) ballet body shaking sway the body have a good figure special training arm movement da ...
- Django运行访问项目出现的问题:DisallowedHost at / Invalid HTTP_HOST header
Django运行访问项目出现的问题:DisallowedHost at / Invalid HTTP_HOST header: DisallowedHost at / Invalid HTTP_HOS ...
- Installation of CarbonData 1.1.0 with Spark 1.6.2
关键词:carbondata spark thrift 数据仓库 [Install thrift 0.9.3] 注意 要装thrift-java必须先装ant . 有人说要装boost,我在cento ...
- js以键值对的方式获取URL的参数
在前端日常的开发中,大多数时候我们只需用js获取到url中的参数即可,这个实现起来也很方便如: function getQueryString(value) { const reg = new Reg ...