Iterating through a merge sorted list

bstapies :

Here is my problem:

Assume the days of the current year are numbered 1 to 365. A Coverage is defined as a range of dates with a coverage start and end/term date. Eg: Cov(1, 31) means the person is covered for something for Jan of this year.

Problem: Given a series of coverage data for a person, we need to find the longest continuous coverage. The coverage may have overlap and/or gaps in coverage. (Given code is in Scala, I used Java to solve).

class Cov(eff: Int, term: Int)
    val coverages = List(Cov(1,20), Cov(21,30), Cov(15,25), Cov(28,40), Cov(50, 60), Cov(61,200))

I could be approaching this question all wrong but I wanted to use mergesort to sort the original array, and then iterate through it, to print the longest term. My mergesort sorts my list. Which is what I wanted. But I am having trouble with how to move on to finding the longest continuous coverage. It wouldn't be just be printing the last index and 2nd to last index so I am lost.

public class Cov {

    private int eff;
    private int term;

    public Cov(int eff, int term) {
        this.eff = eff;
        this.term = term;
    }

    public static void merge(int[] coverage, int eff, int mid, int term) {
        // Creating temporary subarrays
        int leftArray[] = new int[mid - eff + 1];
        int rightArray[] = new int[term - mid];

        // Copying our subarrays into temporaries
        for (int i = 0; i < leftArray.length; i++)
            leftArray[i] = coverage[eff + i];
        for (int i = 0; i < rightArray.length; i++)
            rightArray[i] = coverage[mid + i + 1];

        // Iterators containing current index of temp subarrays
        int leftIndex = 0;
        int rightIndex = 0;

        // Copying from leftArray and rightArray back into array
        for (int i = eff; i < term + 1; i++) {
            // If there are still uncopied elements in R and L, copy minimum of the two
            if (leftIndex < leftArray.length && rightIndex < rightArray.length) {
                if (leftArray[leftIndex] < rightArray[rightIndex]) {
                   coverage[i] = leftArray[leftIndex];
                   leftIndex++;
                } else {
                    coverage[i] = rightArray[rightIndex];
                    rightIndex++;
                }
            } else if (leftIndex < leftArray.length) {
                // If all elements have been copied from rightArray, copy rest of leftArray
                coverage[i] = leftArray[leftIndex];
                leftIndex++;
            } else if (rightIndex < rightArray.length) {
                // If all elements have been copied from leftArray, copy rest of rightArray
                coverage[i] = rightArray[rightIndex];
                rightIndex++;
            }
        }
    }

    public static void mergeSort(int[] coverage, int eff, int term) {
        if (term <= eff) return;

        int mid = (eff + term) / 2;
        mergeSort(coverage, eff, mid);
        mergeSort(coverage, mid + 1, term);
        merge(coverage, eff, mid, term);
    }

    public static void main(String[] args) {
        List<Integer> coverages = new ArrayList<>();
        int coverage[] = { 1, 20, 21, 30, 15, 25, 28, 40, 50, 60, 61, 200 };
        merge(coverage, 0, 5, 11);
        System.out.println(Arrays.toString(coverage));

            for (int i = 0; i < coverage.length - 1; i++) {

            }
        }
    }
Andreas :

To find longest continued coverage, iterate the list and keep track of current coverage period, extending/replacing as needed.

var coverages = List.of(new Cov(1,20), new Cov(21,30), new Cov(15,25),
        new Cov(28,40), new Cov(50, 60), new Cov(61,200));

// Sort coverage periods by start value (eff)
//   (streaming into new list since original list is immutable)
coverages = coverages.stream().sorted(Comparator.comparingInt(Cov::getEff))
        .collect(Collectors.toList());

// Iterate coverage periods and find length of longest continuously covered period
int currEff = -1, currTerm = -1, maxLen = 0;
for (Cov cov : coverages) {
    if (cov.getEff() > currTerm + 1) { // Replace current coverage period if gap detected
        currEff = cov.getEff();
        currTerm = cov.getTerm();
    } else if (currTerm < cov.getTerm()) { // Extend current coverage period if needed
        currTerm = cov.getTerm();
    }
    // Update max if current coverage period is longer than any seen so far
    if (currTerm - currEff >= maxLen)
        maxLen = currTerm - currEff + 1;
}

System.out.println(maxLen); // prints: 151
class Cov {
    private final int eff;
    private final int term;
    public Cov(int eff, int term) {
        this.eff = eff;
        this.term = term;
    }
    public int getEff() {
        return this.eff;
    }
    public int getTerm() {
        return this.term;
    }
    @Override
    public String toString() {
        return "Cov(" + this.eff + "," + this.term + ")";
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297432&siteId=1