贪心算法实现线段覆盖问题

今天在看贪心算法相关的博客时,看到一篇博主给出的线段覆盖问题并没有实际解决线段相互不能覆盖的问题,所以自己想了一个涵盖线段不能相互覆盖条件的最大长度解答

下面直接贴代码:

import java.util.ArrayList;

public class Solution3 {

    public void greedySelect(int num, int[] startPoint, int[] endPoint, boolean[] mark) {
        int[] len = new int[num];
        int totalLength = 0;
        int maxTotalLength = 0;
        for(int i=0;i<num;i++)
            len[i] = endPoint[i] - startPoint[i];
        int[] tmp = new int[num];
        for(int i=0;i<num;i++)
            tmp[i] = len[i];
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<num;i++) {
            int[] used = new int[num];
            boolean flag = true;
            int index = getUnusedMaxValueIndex(tmp, list);
            totalLength += len[index];
            mark[index] = true;
            used[index] = len[index];
            list.add(index);
            for(int k=0;k<num;k++) {
                ArrayList<Integer> backupList = new ArrayList<>();
                for(int b=0;b<used.length;b++)
                    if(used[b] != 0)
                        backupList.add(b);
                if(backupList.size() == 1) {
                    if(startPoint[k] >= endPoint[backupList.get(0)] || endPoint[k] <= startPoint[backupList.get(0)])
                        flag = true;
                    else
                        flag = false;
                }
                else {
                    for(int m=0;m<backupList.size();m++) {
                        if(mark[k] == false) {
                            if(m == 0) {
                                if(endPoint[k] <= startPoint[backupList.get(m)] || (startPoint[k] >= endPoint[backupList.get(m)] && endPoint[k] <= startPoint[backupList.get(m+1)]))
                                    flag = true;
                                else
                                    flag = false;
                            }else if(m == backupList.size()-1) {
                                if(startPoint[k] >= endPoint[backupList.get(m)])
                                    flag = true;
                                else
                                    flag = false;
                            }else {
                                if(startPoint[k] >= endPoint[backupList.get(m)] && endPoint[k] <= startPoint[backupList.get(m+1)])
                                    flag = true;
                                else
                                    flag = false;
                            }
                        }else {
                            flag = false;
                        }
                    }
                }
                if(flag == true) {
                    totalLength += len[k];
                    used[k] = len[k];
                    mark[k] = true;
                }
            }
           if(totalLength > maxTotalLength)
                maxTotalLength = totalLength;
            totalLength = 0;
            for(int a=0;a<num;a++)
                mark[a] = false;
        }
        System.out.println("maxTotalLength:"+maxTotalLength);
    }

    public int getUnusedMaxValueIndex(int[] array, ArrayList<Integer> list) {
        int max = 0;
        int index = 0;
        for(int i=0;i<array.length;i++) {
            if(array[i] > max && !list.contains(i)) {
                max = array[i];
                index = i;
            }
        }
        return index;
    }
}
main函数:
public class Main3 {
    public static  void main(String[] args) {
        int[] startPoint = {2,3,4,5,6,7,8,9,10,11};
        int[] endPoint = {3,5,7,6,9,8,12,10,13,15};
        int len = startPoint.length;
        boolean[] mark = new boolean[len];
        for(int i=0;i<len;i++) {
            mark[i] = false;
        }
        Solution3 s3 = new Solution3();
        s3.greedySelect(len, startPoint, endPoint, mark);
    }
}
    原文作者:贪心算法
    原文地址: https://blog.csdn.net/tj666666/article/details/78797357
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞