Skip to content

Commit cb92bea

Browse files
committed
Add solution #775
1 parent fb4e70f commit cb92bea

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@
586586
769|[Max Chunks To Make Sorted](./0769-max-chunks-to-make-sorted.js)|Medium|
587587
770|[Basic Calculator IV](./0770-basic-calculator-iv.js)|Hard|
588588
773|[Sliding Puzzle](./0773-sliding-puzzle.js)|Hard|
589+
775|[Global and Local Inversions](./0775-global-and-local-inversions.js)|Medium|
589590
783|[Minimum Distance Between BST Nodes](./0783-minimum-distance-between-bst-nodes.js)|Easy|
590591
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
591592
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 775. Global and Local Inversions
3+
* https://leetcode.com/problems/global-and-local-inversions/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums of length n which represents a permutation of all the
7+
* integers in the range [0, n - 1].
8+
*
9+
* The number of global inversions is the number of the different pairs (i, j) where:
10+
* - 0 <= i < j < n
11+
* - nums[i] > nums[j]
12+
*
13+
* The number of local inversions is the number of indices i where:
14+
* - 0 <= i < n - 1
15+
* - nums[i] > nums[i + 1]
16+
*
17+
* Return true if the number of global inversions is equal to the number of local inversions.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums
22+
* @return {boolean}
23+
*/
24+
var isIdealPermutation = function(nums) {
25+
for (let i = 0; i < nums.length; i++) {
26+
if (Math.abs(nums[i] - i) > 1) {
27+
return false;
28+
}
29+
}
30+
return true;
31+
};

0 commit comments

Comments
 (0)