From 59a73b134f2f8ff7e4d9ff5ac13bc936e223bec9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Wed, 25 Sep 2019 10:21:14 +0530 Subject: [PATCH 1/4] update: new problem added --- src/_Problems_/max-consecutive-1s/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/_Problems_/max-consecutive-1s/index.js diff --git a/src/_Problems_/max-consecutive-1s/index.js b/src/_Problems_/max-consecutive-1s/index.js new file mode 100644 index 00000000..ae20105f --- /dev/null +++ b/src/_Problems_/max-consecutive-1s/index.js @@ -0,0 +1,21 @@ +function findMaxConsecutive1s(arr) { + let count = 0; + let max = 0; + const length = arr.length; + + for (let i = 0; i < length; i += 1) { + if (arr[i] === 1) { + count += 1; + } else if (arr[i] === 0) { + if (count > max) max = count; + count = 0; + } + + if (i === length - 1) { + if (count > max) max = count; + } + } + return max; +} + +console.log(findMaxConsecutive1s([1, 1, 0, 1])); From cb5bc8a9d0e82e3f9211433864e91c670273713f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Wed, 25 Sep 2019 10:26:07 +0530 Subject: [PATCH 2/4] update: cleanup and comments --- src/_Problems_/max-consecutive-1s/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/_Problems_/max-consecutive-1s/index.js b/src/_Problems_/max-consecutive-1s/index.js index ae20105f..679d4c0c 100644 --- a/src/_Problems_/max-consecutive-1s/index.js +++ b/src/_Problems_/max-consecutive-1s/index.js @@ -1,3 +1,11 @@ +/** + * Find the length of maximum consecutive 1s in an array of o & 1 + * Input: [1, 0, 1, 1, 0] O/P - 2 + * Input: [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1] O/P - 4 + * + * Solved in O(n) + */ + function findMaxConsecutive1s(arr) { let count = 0; let max = 0; @@ -17,5 +25,3 @@ function findMaxConsecutive1s(arr) { } return max; } - -console.log(findMaxConsecutive1s([1, 1, 0, 1])); From 264980f37d155b67288539d42311f84bb73544fd Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Wed, 25 Sep 2019 11:59:39 +0530 Subject: [PATCH 3/4] update: reduced a comparision on every iteration --- src/_Problems_/max-consecutive-1s/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/_Problems_/max-consecutive-1s/index.js b/src/_Problems_/max-consecutive-1s/index.js index 679d4c0c..c3ffdc8f 100644 --- a/src/_Problems_/max-consecutive-1s/index.js +++ b/src/_Problems_/max-consecutive-1s/index.js @@ -18,10 +18,7 @@ function findMaxConsecutive1s(arr) { if (count > max) max = count; count = 0; } - - if (i === length - 1) { - if (count > max) max = count; - } } + if (count > max) max = count; return max; } From ec5d2f6a392d2c34c6ee5fca2131959abe0e4df3 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Wed, 25 Sep 2019 12:00:35 +0530 Subject: [PATCH 4/4] fix: comment --- src/_Problems_/max-consecutive-1s/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_Problems_/max-consecutive-1s/index.js b/src/_Problems_/max-consecutive-1s/index.js index c3ffdc8f..18a5058a 100644 --- a/src/_Problems_/max-consecutive-1s/index.js +++ b/src/_Problems_/max-consecutive-1s/index.js @@ -1,7 +1,7 @@ /** * Find the length of maximum consecutive 1s in an array of o & 1 * Input: [1, 0, 1, 1, 0] O/P - 2 - * Input: [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1] O/P - 4 + * Input: [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1] O/P - 5 * * Solved in O(n) */