Skip to content

Commit ba6645a

Browse files
committed
refactor(longestSubSeq): add cmp and change name
1 parent b21b5e5 commit ba6645a

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/searching/longest-increasing-subsequence.js renamed to src/searching/longest-subsequence.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@
1010
* @private
1111
* @param {Array} array The array in which the largest
1212
* element should be found.
13-
* @param {Function} cmp Function used for comparison.
1413
* @return {Number} index of the first largest element
1514
*/
16-
function max(array, cmp) {
15+
function max(array) {
1716
if (!array || !array.length) {
1817
return -1;
1918
}
20-
if (!cmp) {
21-
cmp = function (a, b) { return a - b; };
22-
}
2319
var maxIdx = 0;
2420
for (var i = 1; i < array.length; i += 1) {
25-
if (cmp(array[maxIdx], array[i]) < 0) {
21+
if (array[maxIdx].distance < array[i].distance) {
2622
maxIdx = i;
2723
}
2824
}
@@ -33,8 +29,8 @@
3329
* Default comparison method.
3430
* @private
3531
*/
36-
function cmp(a, b) {
37-
return a.distance - b.distance;
32+
function asc(a, b) {
33+
return a - b;
3834
}
3935

4036
/**
@@ -46,12 +42,12 @@
4642
* @param {Array} array The input array.
4743
* @return {Object} Graph represented with list of neighbours.
4844
*/
49-
function buildDag(array) {
45+
function buildDag(array, cmp) {
5046
var result = [];
5147
for (var i = 0; i < array.length; i += 1) {
5248
result[i] = [];
5349
for (var j = i + 1; j < array.length; j += 1) {
54-
if (array[i] < array[j]) {
50+
if (cmp(array[i], array[j]) < 0) {
5551
result[i].push(j);
5652
}
5753
}
@@ -87,7 +83,7 @@
8783
neighboursDistance[i] = find(dag, neighbours[i]);
8884
}
8985

90-
maxDist = max(neighboursDistance, cmp);
86+
maxDist = max(neighboursDistance);
9187
maxNode = neighbours[maxDist];
9288
distance = 1 + neighboursDistance[maxDist].distance;
9389
find.memo[node] = result = {
@@ -114,15 +110,16 @@
114110
* @param {Array} array Input sequence.
115111
* @return {Array} Longest increasing subsequence.
116112
*/
117-
return function (array) {
113+
return function (array, cmp) {
114+
cmp = cmp || asc;
118115
var results = [];
119-
var dag = buildDag(array);
116+
var dag = buildDag(array, cmp);
120117
var maxPath;
121118
find.memo = [];
122119
for (var i = 0; i < array.length; i += 1) {
123120
results.push(find(dag, i));
124121
}
125-
maxPath = results[max(results, cmp)];
122+
maxPath = results[max(results)];
126123
results = [];
127124
while (maxPath) {
128125
results.push(array[maxPath.node]);
@@ -133,3 +130,4 @@
133130
})();
134131

135132
})(typeof window === 'undefined' ? module.exports : window);
133+

test/searching/longest-increasing-subsequence.spec.js renamed to test/searching/longest-subsequence.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var longestSubsequence =
44
require('../../src/searching/' +
5-
'longest-increasing-subsequence')
5+
'longest-subsequence')
66
.longestSubsequence;
77

88
describe('longest subsequence', function () {
@@ -33,4 +33,15 @@ describe('longest subsequence', function () {
3333
expect(longestSubsequence(sequence).toString())
3434
.toBe([2, 3, 6, 9, 11].toString());
3535
});
36+
37+
it('should work with a custom comparator', function () {
38+
var cmp = function (a, b) {
39+
return b - a;
40+
};
41+
var seq = [1, 2, -1];
42+
var result = longestSubsequence(seq, cmp);
43+
expect(result.length).toBe(2);
44+
expect(result).toEqual([1, -1]);
45+
});
3646
});
47+

0 commit comments

Comments
 (0)