Skip to content

Commit 870e071

Browse files
author
baochau.dinh
committed
js-concepts: leetcode prob.525 - Contiguous array, parallel with async utility
1 parent 3b8f40e commit 870e071

File tree

5 files changed

+18638
-3767
lines changed

5 files changed

+18638
-3767
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Given a binary array of [0, 1], return the maximum length of a contiguous subarray with equal number 1 and 0
3+
*
4+
* 1 0 0 1 0 1
5+
* 0 0 0 1 0 1
6+
*
7+
*/
8+
9+
var findMaxLength = function(nums) {
10+
if (nums.length < 2) return 0;
11+
let sum = [];
12+
let map = {};
13+
let result = 0;
14+
15+
for (let i = 0; i < nums.length; i++) {
16+
if (nums[i] === 0) sum[i] = (sum[i - 1] || 0) - 1;
17+
if (nums[i] === 1) sum[i] = (sum[i - 1] || 0) + 1;
18+
}
19+
console.log('sum: ', sum);
20+
21+
for (let i = 0; i < sum.length; i++) {
22+
if (map[sum[i]] === undefined) {
23+
map[sum[i]] = i;
24+
}
25+
}
26+
console.log('map: ', map);
27+
28+
for (let i = 0; i < sum.length; i++) {
29+
if (sum[i] === 0) {
30+
result = Math.max(result, i + 1);
31+
} else {
32+
result = Math.max(result, i - map[sum[i]])
33+
}
34+
}
35+
36+
return result;
37+
};
38+
39+
console.log(findMaxLength([1, 0, 0, 1, 0, 1]));

Parallel/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const async = require('async');
2+
3+
async.parallel([
4+
function(callback) {
5+
setTimeout(function() {
6+
console.log('Task one');
7+
callback(null, 1);
8+
}, 200);
9+
},
10+
11+
function(callback) {
12+
setTimeout(function() {
13+
console.log('Task two');
14+
callback(null, 2);
15+
}, 100);
16+
}
17+
],
18+
function(err, results) {
19+
console.log(results);
20+
});

0 commit comments

Comments
 (0)