Counting viable sublist lengths from an array.
Posted
by Ben B.
on Stack Overflow
See other posts from Stack Overflow
or by Ben B.
Published on 2010-04-01T17:07:24Z
Indexed on
2010/04/01
17:13 UTC
Read the original article
Hit count: 305
This is for a genetic algorithm fitness function, so it is important I can do this as efficiently as possible, as it will be repeated over and over.
Lets say there is a function foo(int[] array) that returns true if the array is a "good" array and false if the array is a "bad" array. What makes it good or bad does not matter here.
Given the full array [1,6,8,9,5,11,45,16,9], lets say that subarray [1,6,8] is a "good" array and [9,5,11,45] is a "good" array. Furthermore [5,11,45,16,9] is a "good" array, and also the longest "good" subarray. Notice that while [9,5,11,45] is a "good" array, and [5,11,45,16,9] is a "good" array, [9,5,11,45,16,9] is a "bad" array.
We wants the length counts of all "good" arrays, but not subarrays of "good" arrays. Furthermore, as described above, a "good" array might begin in the middle of another "good" array, but the combination of the two might be a "bad" array.
© Stack Overflow or respective owner