zoukankan      html  css  js  c++  java
  • 1024. Video Stitching

    //使用java dfs

        public int videoStitching(int[][] clips, int T) {
            //bfs
            Queue<Integer> queue = new LinkedList<>();
            Set<Integer> visited = new HashSet<>();
            for (int i = 0;i < clips.length; i ++)
                if (clips[i][0] == 0){
                    queue.offer(clips[i][1]);
                    queue.offer(1);
                    visited.add(clips[i][0]* 1000 + clips[i][1]);
                }
            if (queue.isEmpty())
                return -1;
            
            int end = -1, step = -1;
            while ( !queue.isEmpty() ){
                end = queue.poll();
                step = queue.poll();
                if (end >= T)
                    return step;
                for (int i = 0; i< clips.length; i++){
                    if (!visited.contains(clips[i][0]* 1000 + clips[i][1]))
                        if (end >= clips[i][0]){
                            queue.offer(clips[i][1]);
                            queue.offer(step + 1);
                            visited.add(clips[i][0]* 1000 + clips[i][1]);
                        }
                }
            }
            return -1;
        }
    

    python dp

    def videoStitching(self, clips: List[List[int]], T: int) -> int:
            #dp[i] 代表 到i结尾时间的最小个数
            clips.sort()
            n = len(clips)
            dp = [float('inf')]* n
            if clips[0][0] != 0:
                return -1
            for i in range(n):
                if clips[i][0] == 0:
                    dp[i] = 1
                else:
                    break
            for i in range(n):
                for j in range(i):
                    if dp[j] != float('inf') and clips[j][1] >= clips[i][0] :
                        dp[i] = min(dp[i], dp[j] + 1)
            res = float('inf')
            for i in range(n):
                if clips[i][1] >= T:
                    res = min(res, dp[i])
            return res if res != float('inf') else - 1
    
  • 相关阅读:
    Java构造方法之间的调用
    JavaNote
    微信小程序-智能机器人
    微信小程序-今日头条案例
    微信小程序-记账本
    51job爬虫
    Xcode文件目录选中变成白色, 解决方案
    Mac通过以太网共享网络
    Mac系统Safari浏览器启动无图模式
    iOS9.0之后不支持http请求解决方案
  • 原文地址:https://www.cnblogs.com/whyaza/p/10668671.html
Copyright © 2011-2022 走看看