Рет қаралды 1,604
MaxNonoverlappingSegments in the first exercise in the Greedy algorithms lesson on Codility. The aim is to find the maximum number of segments that don't overlap in a set of segments defined in two given arrays.
The segments are pre-sorted by end position. I keep track of the current position, which is the end of each segment that I consider, I then scan over the arrays to find lines that start after the current position and count the segments when I find a line that doesn't overlap.
This solution scores 100% on Codility.