I have a binary array and would like to count the longest number of 1s in a row with this function. In particular, when I use this value for nums like [1,1,0,1]
it counts correctly.
let nums = [1, 1, 0, 1, 1, 1];
var findMaxConsecutiveOnes = function(nums) {
let val = 0;
if (nums.length >= 1) {
console.log(nums.sort().reverse());
for (i = 0; i <= nums.length - 1; i ) {
if (nums[i] === nums[i 1]) {
val ;
} else {
console.log(val);
return val;
}
}
console.log(val);
return 0;
}
};
findMaxConsecutiveOnes(nums)
CodePudding user response:
This is a simple problem with Array.prototype.reduce
:
const maxConsecutiveOnes = (ns) => ns .reduce (
({max, curr}, n) => n == 1 ? {curr: curr 1, max: curr >= max ? curr 1 : max} : {max, curr: 0},
{max: 0, curr: 0}
) .max
console .log (maxConsecutiveOnes ([1, 1, 0, 1]))
console .log (maxConsecutiveOnes ([1, 1, 0, 1, 1, 1]))
We fold the array into a {max: Number, curr: Number}
object, starting with {max: 0, curr: 0}
. At every step, if we have a 0
, we reset curr
to 0
and keep max
. Otherwise we increment curr
, and if that's larger than max
, we use it for max
as well. At the end, we just extract the max
property of our accumulated object.
Thus for example, we would go through these steps for [1, 1, 0, 1, 1, 1, 0, 1]
:
n | accumulator | notes |
---|---|---|
- | {max: 0, curr: 0} |
initial accumulator value |
1 | {max: 1, curr: 1} |
increment curr and max |
1 | {max: 2, curr: 2} |
increment curr and max |
0 | {max: 2, curr: 0} |
reset curr to 0 |
1 | {max: 2, curr: 1} |
increment curr |
1 | {max: 2, curr: 2} |
increment curr |
1 | {max: 3, curr: 3} |
increment curr and max |
0 | {max: 3, curr: 0} |
reset curr to 0 |
1 | {max: 3, curr: 1} |
increment curr |
- | - | now we extract max , and return 3 |
CodePudding user response:
I think it will work if you sort it like you do for your console.log() on line 6. If you don't want to alter you array, you should try with a copy that you sort. I think there are cases where you leave the loop. [1100111] makes you return val when you are at the zeros but you still have more ones after. I'm not sure though, I didn't take time to check it.
CodePudding user response:
How about something nice and simple in a forEach loop:
const binaryArray = [1, 1, 0, 1, 1, 1]
const getMaxConsecutiveOnes = (arr) => {
let countOfOnes = 0
let maxConsecutiveOnes = 0
arr.forEach(num => {
if (num === 1) {
countOfOnes
} else if (countOfOnes > maxConsecutiveOnes) {
maxConsecutiveOnes = countOfOnes
countOfOnes = 0
}
})
// Final check of which is longer to account for end of array
return maxConsecutiveOnes > countOfOnes ? maxConsecutiveOnes : countOfOnes
}
console.log(getMaxConsecutiveOnes(binaryArray)) // expected output: 3
CodePudding user response:
If I understand correctly, you need to find the maximum consecutive 1s in the binary array.
If not, ignore.
If yes,
As per your current code,
Lets say, you have the 3 ones. 1,1,1 . The one at index 0 is equal to index 1. val becomes 1. Index 1 is equal to index 2. Val now becomes 2. It stops. Eventhough you have three consecutive 1s, you are printing only 2.
Now coming to the case of 1,1,0,1
You should not be sorting it. Firstly it will group together all the 1s. In the case of [1,1,0,1] => when you sort, it is [1,1,1,0] => so it prints 3-1 = 2
In case of [1, 1, 0, 1, 1, 1]
It sorts to [1,1,1,1,1,0]. So prints 5-1=4
Try to do it without sort. One advantage is that you will not lose the max value. Second, you do not iterate the array twice(in case of sort, internally it iterates)
So, you can perhaps try something to map through each element and keep a track of consecutive 1s. Keep a counter too. If you encounter 0, reset the counter. Also after incrementing and before resetting, check if the current sequence is greater than the max
This is one possibility. There could be more ways.
let nums = [1, 1, 0, 1];
var findMaxConsecutiveOnes = function(nums) {
let val = 0;
let max = 0;
nums.forEach(el => {
if(el == 1){
val ;
max = val > max ? val : max
}
else{
max = val > max ? val : max
val = 0;
}
})
return max
};
findMaxConsecutiveOnes(nums)