【1024. 视频拼接】贪心

复制代码
class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ans = solution.videoStitching(new int[][]{
                {8,10},{17,39},{18,19},{8,16},{13,35},{33,39},{11,19},{18,35}
        },20);
        System.out.println(ans);
    }
    public int videoStitching(int[][] clips, int time) {
        int len = clips.length;
        int t = time;
        boolean first = true;
        int ans = 0;
        while (t != 0) {
            int index = selectOne(clips,t,first);
            if (index == -1){
                return -1;
            }
            first = false;
            t = clips[index][0];
            ans++;
        }
        return ans;
    }

    public int selectOne(int[][] clips,int t,boolean first){
        int dis = Integer.MAX_VALUE;
        int ans = -1;
        for(int i = 0;i<clips.length;i++){
            int[] arr = clips[i];
            if(arr[1] >= t && ( (first && arr[0] <= t) || (!first && arr[0] < t) )&& arr[0] < dis) {
                dis = arr[0];
                ans = i;
            }
        }
        return ans;
    }

}
复制代码

 

本文作者:love

本文链接:https://www.cnblogs.com/fishcanfly/p/16351056.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   fishcanfly  阅读(113)  评论(0编辑  收藏  举报
//雪花飘落效果
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起