Skip to content

Commit 9ddaecc

Browse files
committed
Updated README and partially solved (timing out on heavy tests) the countInversions.
1 parent 70e754f commit 9ddaecc

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ Note: Few codes were solved in C# (I've written them a long time ago), I'll conv
7575

7676
### [Arrays](data-structures/arrays)
7777

78-
- [Array Manipulation](data-structures/arrays/arrayManipulation.js)
7978
- [2D Array - DS](data-structures/arrays/hoursglassSum.js)
79+
- [Array Manipulation](data-structures/arrays/arrayManipulation.js)
8080
### [Linked Lists](data-structures/linked-lists)
8181

8282
- [Find Merge Point Of Two Lists](data-structures/linked-lists/findMergeNode.js)
8383
- [Insert A Node At A Specific Position In A Linked List](data-structures/linked-lists/insertNodeAtPosition.js)
84-
- [Reverse A Doubly Linked List](data-structures/linked-lists/reverse.js)
8584
- [Inserting a Node Into a Sorted Doubly Linked List](data-structures/linked-lists/sortedInsert.js)
85+
- [Reverse A Doubly Linked List](data-structures/linked-lists/reverse.js)
8686

8787
### [Trees](data-structures/trees)
8888

89-
- [Tree: Height of a Binary Tree](data-structures/trees/height.js)
9089
- [Binary Search Tree : Lowest Common Ancestor](data-structures/trees/lca.js)
90+
- [Tree: Height of a Binary Tree](data-structures/trees/height.js)
91+
9192
## [Functional Programming](functional-programming)
9293

9394
- [Solve Me First FP](functional-programming/introduction/solveMeFirst.cs)
@@ -96,13 +97,15 @@ Note: Few codes were solved in C# (I've written them a long time ago), I'll conv
9697
## [Tutorials](tutorials)
9798

9899
### [Cracking The Code Interview](tutorials/cracking-the-code-interview)
99-
100+
Merge Sort: Counting Inversions
101+
- [Arrays: Left Rotation](tutorials/cracking-the-code-interview/rotLeft.js)
102+
- [Hash Tables: Ice Cream Parlor](tutorials/cracking-the-code-interview/whatFlavors.js)
100103
- [Hash Tables: Ransom Note Solution](tutorials/cracking-the-code-interview/checkMagazine.js)
101-
- [Sorting: Comparator](tutorials/cracking-the-code-interview/compare.js)
102-
- [Sorting: Bubble Sort](tutorials/cracking-the-code-interview/countSwaps.js)
103-
- [Recursion: Fibonacci Numbers](tutorials/cracking-the-code-interview/fibonacci.js)
104104
- [Linked Lists: Detect a Cycle](tutorials/cracking-the-code-interview/has_cycle.js)
105+
- [Merge Sort: Counting Inversions](tutorials/cracking-the-code-interview/countInversions.js)
106+
- [Recursion: Fibonacci Numbers](tutorials/cracking-the-code-interview/fibonacci.js)
107+
- [Sorting: Bubble Sort](tutorials/cracking-the-code-interview/countSwaps.js)
108+
- [Sorting: Comparator](tutorials/cracking-the-code-interview/compare.js)
105109
- [Strings: Making Anagrams](tutorials/cracking-the-code-interview/makeAnagram.js)
106110
- [Time Complexity: Primality](tutorials/cracking-the-code-interview/primality.js)
107-
- [Arrays: Left Rotation](tutorials/cracking-the-code-interview/rotLeft.js)
108-
- [Hash Tables: Ice Cream Parlor](tutorials/cracking-the-code-interview/whatFlavors.js)
111+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
// Currently failing at the heavy tests ;_;
5+
// It's clear that I have to find a way to avoid updating the original array, but I need some free time to think about it
6+
7+
function countInversions(arr) {
8+
const binarySearch = (o, v, i) => {
9+
let h = o.length,
10+
l = i - 1,
11+
m;
12+
while (h - l > 1)
13+
if (o[m = h + l >> 1] < v) l = m;
14+
else h = m;
15+
return h;
16+
};
17+
let inversions = 0;
18+
for (let l = arr.length, i = arr.length; --i;)
19+
if (arr[i - 1] > arr[i]) {
20+
const insert = binarySearch(arr, arr[i - 1], i);
21+
for (let j = i - 1; ++j < insert;)
22+
[arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
23+
inversions += insert - i;
24+
}
25+
return inversions;
26+
}

0 commit comments

Comments
 (0)