讨论/题目交流/🏆 第 159 场力扣周赛/
🏆 第 159 场力扣周赛

欢迎小伙伴们在这里交流分享你的参赛心得以及体验。【前往竞赛

image.png

缀点成线
删除子文件夹
替换子串得到平衡字符串
规划兼职工作

展开讨论

第4题抛一个题解(二分+01背包+离散化):
很简单,先按end排序得到每个位置的最大值,首先得到当前end对应的离散值i(end->i 之后以这个方式表示),因为[start,end]区别肯定是不允许有任务的,也就是要找到end2 <= start,假设(end2->j),那么递推就好推了dp[i]=max(dp[i],当前区间的profit + dp[j]),可是呢,有可能以上一个end3的值更大,既dp[i]=max(dp[i],dp[i-1]),那么这个区间的值不要也罢了。。就是这样推下来

import org.omg.PortableServer.POA;

import java.util.*;

public class test27 {

    class Point
    {
        int start;
        int end;
        int profit;
    }

    class  Mycom implements Comparator<Point>
    {

        @Override
        public int compare(final Point o1, final Point o2) {

            if(o1.end < o2.end) return -1;
            else if (o1.end == o2.end && o1.start < o2.start) return -1;
            return 1;
        }
    }

    public int search(int[] xs,int key)
    {
        int left = 0;
        int right = xs.length - 1;
        int mid;

        while (left <= right)
        {
            mid = (left + right) / 2;
            if(xs[mid] > key) right = mid - 1;
            else
            {
                left = mid + 1;
            }
        }
        left = left - 1;

        return left;
    }

    public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {

        int ans = 0;
        int index = 0;

        int length = startTime.length;
        Point[] ps = new Point[length];
        int [] dps;
        int [] ends;
        Set<Integer> set = new HashSet<>();
        Map<Integer,Integer> map;

        for(int i = 0;i < length;i++)
        {
            ps[i] = new Point();
            ps[i].start = startTime[i];
            ps[i].end = endTime[i];
            ps[i].profit = profit[i];

            if(set.contains(ps[i].end)) continue;
            set.add(ps[i].end);
        }
        dps = new int[set.size()];
        ends = new int[set.size()];
        map = new HashMap<>(set.size());

        for(int i = 0; i < dps.length;i++) dps[i] = 0;

        Arrays.sort(ps,new Mycom());

        for(int i = 0;i < length;i++)
        {
            if(map.containsKey(ps[i].end)) continue;
            map.put(ps[i].end,index);
            ends[index++] = ps[i].end;
        }

        for(int i = 0;i < length;i++)
        {
            int lag = search(ends,ps[i].start);
            int endindex = map.get(ps[i].end);

            if(lag == -1)
            {
                dps[endindex] = Math.max(dps[endindex],ps[i].profit);
            }
            else
            {
                dps[endindex] = Math.max(dps[endindex],ps[i].profit + dps[lag]);
            }

            if(endindex - 1 >= 0)
            {
                dps[endindex] = Math.max(dps[endindex - 1],dps[endindex]);
            }

            ans = Math.max(dps[endindex],ans);
        }

        return ans;
    }

    public static void main(String[] args) {

        test27 of = new test27();

        int [] startTime = {4,2,4,8,2};
        int [] endTime = {5,5,5,10,8};
        int [] profit = {1,2,8,10,4};
        System.out.println(of.jobScheduling(startTime,endTime,profit));
//        System.out.println(of.search(array,0));
//        System.out.println(of.search(array,1));
//        System.out.println(of.search(array,2));
//        System.out.println(of.search(array,3));
//        System.out.println(of.search(array,4));
//        System.out.println(of.search(array,5));
//        System.out.println(of.search(array,6));
    }
}

展开全部 16 讨论