zoukankan      html  css  js  c++  java
  • 搜狗笔试

    第三题

    package sougo;
    
    import org.junit.Test;
    
    import java.util.*;
    
    public class Main01 {
        class Interval {
            int start;
            int end;
    
            Interval(int start, int end) {
                this.start = start;
                this.end = end;
            }
        }
        public boolean[] bfs(List<Integer>[] g, int n, int start) {
            boolean[] res = new boolean[n];
            boolean[] used = new boolean[n];
            Queue<Integer> q =  new LinkedList<>();
            q.offer(start);used[start] = true;
            while(!q.isEmpty()) {
                int cur = q.poll();
                res[cur] = true;
                for(int i=0; i < g[cur].size(); i++) {
                    int item = g[cur].get(i);
                    if(used[item] == false) {
                        q.offer(item);
                        used[item] = true;
                    }
    
                }
            }
            return res;
        }
        public Interval trim (int N, int M, Interval[] conn) {
            // write code here
            int n = N+2;
            List<Integer>[] g = new ArrayList[n];
            for(int i=0; i < n; i++) g[i] = new ArrayList();
            for(int i=0;  i < M; i++) {
                //System.out.println(i);
                int a = conn[i].start, b = conn[i].end;
                if(b == -1) {
                    b = N+1;
                }
                g[a].add(b);
                //g[b].add(a);
            }
            boolean[] res1 = bfs(g, n, 0);
    
            g = new ArrayList[n];
            for(int i=0; i < n; i++) g[i] = new ArrayList();
            for(int i=0;  i < M; i++) {
                //System.out.println(i);
                int a = conn[i].start, b = conn[i].end;
                if(b == -1) {
                    b = N+1;
                }
                //g[a].add(b);
                g[b].add(a);
            }
    
            boolean[] res2 = bfs(g, n, N+1);
    
            int res = 0;int sum = 0, mod = 100000007;
            for(int i=1; i <= N; i++) {
                if(res1[i] && res2[i]) {
                    res++;
                    sum = (sum + i) % mod;
                }
            }
            return new Interval(res, sum);
        }
    //3,4,[[0,1],[0,2],[2,-1],[2,1]]
        @Test
        public void test() {
    
            int N = 3;
            int M = 4;
            Interval[] conn = new Interval[M];
            conn[0] = new Interval(0,1);
            conn[1] = new Interval(0,2);
            conn[2] = new Interval(2,-1);
            conn[3] = new Interval(2,1);
            Interval trim = trim(N, M, conn);
            System.out.println(trim.start +" " + trim.end);
    
        }
    }
    
    
    
  • 相关阅读:
    docker启动:Got permission denied while trying to connect to the Docker daemon
    Centos7上安装docker
    Docker个人理解总结
    IDEA 的Class not found: "..."Empty test suite
    启动总是提示:Process finished with exit code 0
    配置xml:url is not registered
    MD5加密与Hash加密
    java线程安全问题之静态变量、实例变量、局部变量
    Dockerfile 常用指令
    通过 Service 访问 Pod
  • 原文地址:https://www.cnblogs.com/lixyuan/p/13736908.html
Copyright © 2011-2022 走看看