Find Max Slice Array | Javascript

I need to find the maximum slice of an array that contains no more than two different numbers.

Here is my array [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]

My thought process is to find numbers that don't repeat and return their index to a new array.

Here is what I still have:

 function goThroughInteger(number) { var array = []; //iterate the array and check if number is not repeated number.filter(function (element, index, number) { if(element != number[index-1] && element != number[index+1]) { array.push(index); return element; } }) console.log(array); } goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]); 

I'm not sure where to go next, I struggle to understand the question of what it is - to find the maximum cut that contains no more than two different numbers - it bothers me.

+5
source share
3 answers

A one-cycle solution that checks the latest values ​​and increments the counter.

 function getLongestSlice(array) { var count = 0, max = 0, temp = []; array.forEach(function (a) { var last = temp[temp.length - 1]; if (temp.length < 2 || temp[0].value === a || temp[1].value === a) { ++count; } else { count = last.count + 1; } if (last && last.value === a) { last.count++; } else { temp.push({ value: a, count: 1 }); temp = temp.slice(-2); } if (count > max) { max = count; } }); return max; } console.log(getLongestSlice([58, 800, 0, 0, 0, 356, 8988, 1, 1])); // 4 console.log(getLongestSlice([58, 800, 0, 0, 0, 356, 356, 8988, 1, 1])); // 5 console.log(getLongestSlice([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8])); // 10 
+5
source

This is a possible solution with complexity O (nΒ²) (as pointed out by @le_m in comments)

 goThroughInteger = list => { let scores = list.reduce((slices, num, pos) => { let valid = [num] let count = 0; for (let i=pos; i<list.length; i++) { if (valid.indexOf(list[i]) == -1) { if (valid.length < 2) { valid.push(list[i]) count++ } else { break } } else { count++ } } slices[pos] = {pos, count} return slices }, []) scores.sort((a,b) => b.count - a.count) let max = scores[0] return list.slice(max.pos, max.pos + max.count) } console.log(goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8])) console.log(goThroughInteger([58, 800, 0, 0, 0, 356, 8988, 1, 1])) 

It calculates the β€œscore” at each position in the input list, counting the length of the sequence of no more than two different values. Then it gets the result with the highest result and extracts a fragment from the original list based on this information.

It can definitely be cleaned and optimized, but I think this is a good starting point.

+3
source
  function goThroughInteger(array) { var solutionArray = []; var max = 0; for (var i = 0; i <= array.length; i++) { for (var j = i + 1; j <= array.length; j++) { var currentSlice= array.slice(i,j); var uniqSet = [...new Set(currentSlice)]; if(uniqSet.length <3) { if(currentSlice.length>max) { max= currentSlice.length; } } } } console.log(max); } goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]); 

This solution checks all possible fragments of the array, checks to see if it has at most two different numbers, and finally displays the length of the longest fragment.

+3
source

Source: https://habr.com/ru/post/1265434/


All Articles