开发者社区> 问答> 正文

遍历合并排序列表

这是我的问题:

假设当前年份的天数为1到365。Coverage定义为日期范围,包括Coverage的开始日期和结束日期/任期。例如:Cov(1, 31)表示该人在今年1月的疾病保险。

问题:给定一个人的一系列覆盖范围数据,我们需要找到最长的连续覆盖范围。覆盖范围可能在覆盖范围上有重叠和/或缺口。(鉴于代码在Scala中,我使用Java来解决)。

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))
    ```
我可能会完全错误地解决此问题,但我想使用mergesort对原始数组进行排序,然后对其进行迭代以打印最长的期限。我的mergesort对我的列表进行排序。这就是我想要的。但是我在寻找最长的连续覆盖范围方面遇到了麻烦。不仅仅是打印最后一个索引和第二到最后一个索引,所以我迷路了。

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++) {

        }
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 18:57:34 463 0
1 条回答
写回答
取消 提交回答
  • 要找到最长的续保范围,请遍历列表并跟踪当前的承保期,并根据需要扩展/替换。

    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 + ")";
        }
    }
    

    回答来源:Stack Overflow

    2020-03-26 18:58:07
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载