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

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

下面直接贴代码:

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;iint[] tmp = new int[num];
        for(int i=0;i list = new ArrayList<>();
        for(int i=0;iint[] 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 backupList = new ArrayList<>();
                for(int b=0;bif(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;mif(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;afalse;
        }
        System.out.println("maxTotalLength:"+maxTotalLength);
    }

    public int getUnusedMaxValueIndex(int[] array, ArrayList list) {
        int max = 0;
        int index = 0;
        for(int i=0;iif(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;ifalse;
        }
        Solution3 s3 = new Solution3();
        s3.greedySelect(len, startPoint, endPoint, mark);
    }
}

你可能感兴趣的:(菜鸟java入门)