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);
    
        }
    }
    
    
    
  • 相关阅读:
    PowerMockito
    java--树封装
    plugin--Lombok
    Mysql--sql
    Oracle--sql
    hive--分区表和分桶表
    hive支持的数据类型和存储格式
    HashMap
    golang 创建 tun 设备
    golang ctrie demo
  • 原文地址:https://www.cnblogs.com/lixyuan/p/13736908.html
Copyright © 2011-2022 走看看