GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com

import java.util.*; /** * 815. Bus Routes * BFS * @author LBW */ public class BusRoutes { public int numBusesToDestination(int[][] routes, int source, int target) { if (source == target) return 0; List> stations = new ArrayList<>(); List> graph = new ArrayList<>(); int m = routes.length; for (int i = 0; i < m ; i++) { stations.add(new HashSet<>()); for (int j = 0; j < routes[i].length; j++) { stations.get(i).add(routes[i][j]); } graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { if (interact(stations.get(i), stations.get(j))) { graph.get(i).add(j); graph.get(j).add(i); } } } boolean[] hasVisited = new boolean[m]; Queue queue = new ArrayDeque<>(); Set targets = new HashSet<>(); for (int i = 0; i < m; i++) { if (stations.get(i).contains(target)) { targets.add(i); } } int cur = 1; for (int i = 0; i < m; i++) { if (stations.get(i).contains(source)) { if (targets.contains(i)) { return cur; } queue.offer(i); hasVisited[i] = true; } } while (!queue.isEmpty()) { int size = queue.size(); cur += 1; for (int i = 0; i < size; i++) { int bus = queue.poll(); for (int next: graph.get(bus)) { if (targets.contains(next)) return cur; if (!hasVisited[next]) { queue.offer(next); hasVisited[next] = true; } } } } return -1; } private boolean interact(Set set1, Set set2) { Set result = new HashSet<>(set1); result.retainAll(set2); return !result.isEmpty(); } }
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.