|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class AccessSystem { |
| 4 | + Map<String, List<String>> children; |
| 5 | + Map<String, String> parents; |
| 6 | + Set<String> allRegions; |
| 7 | + Map<String, Set<String>> grants; |
| 8 | + Map<String, Set<String>> revokes; |
| 9 | + |
| 10 | + public AccessSystem(String[][] edges) { |
| 11 | + children = new HashMap<>(); |
| 12 | + parents = new HashMap<>(); |
| 13 | + allRegions = new HashSet<>(); |
| 14 | + grants = new HashMap<>(); |
| 15 | + revokes = new HashMap<>(); |
| 16 | + |
| 17 | + for (String[] edge : edges) { |
| 18 | + String parent = edge[0], child = edge[1]; |
| 19 | + |
| 20 | + children.putIfAbsent(parent, new ArrayList<>()); |
| 21 | + children.get(parent).add(child); |
| 22 | + |
| 23 | + parents.put(child, parent); |
| 24 | + |
| 25 | + allRegions.add(child); |
| 26 | + allRegions.add(parent); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public void grantAccess(String advertiserId, String regionId) { |
| 31 | + if (!allRegions.contains(regionId)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + List<String> allSubRegions = getAllSubRegions(regionId); |
| 36 | + |
| 37 | + grants.putIfAbsent(advertiserId, new HashSet<>()); |
| 38 | + revokes.putIfAbsent(advertiserId, new HashSet<>()); |
| 39 | + |
| 40 | + for (String region : allSubRegions) { |
| 41 | + grants.get(advertiserId).add(region); |
| 42 | + revokes.get(advertiserId).remove(region); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void revokeAccess(String advertiserId, String regionId) { |
| 47 | + if (!allRegions.contains(regionId)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + List<String> allSubRegions = getAllSubRegions(regionId); |
| 52 | + |
| 53 | + grants.putIfAbsent(advertiserId, new HashSet<>()); |
| 54 | + revokes.putIfAbsent(advertiserId, new HashSet<>()); |
| 55 | + |
| 56 | + for (String region : allSubRegions) { |
| 57 | + grants.get(advertiserId).remove(region); |
| 58 | + revokes.get(advertiserId).add(region); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public boolean checkAccess(String advertiserId, String regionId) { |
| 63 | + if (!allRegions.contains(regionId)) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + String curr = regionId; |
| 68 | + |
| 69 | + Set<String> grantedRegions = grants.getOrDefault(advertiserId, Collections.emptySet()); |
| 70 | + Set<String> revokedRegions = revokes.getOrDefault(advertiserId, Collections.emptySet()); |
| 71 | + |
| 72 | + while (curr != null) { |
| 73 | + if (revokedRegions.contains(curr)) { return false; } |
| 74 | + if (grantedRegions.contains(curr)) { return true; } |
| 75 | + |
| 76 | + curr = parents.get(curr); |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + private List<String> getAllSubRegions(String regionId) { |
| 83 | + if (!allRegions.contains(regionId)) { |
| 84 | + return Collections.emptyList(); |
| 85 | + } |
| 86 | + |
| 87 | + List<String> result = new ArrayList<>(); |
| 88 | + Queue<String> q = new LinkedList<>(); |
| 89 | + q.offer(regionId); |
| 90 | + |
| 91 | + while (!q.isEmpty()) { |
| 92 | + String curr = q.poll(); |
| 93 | + result.add(curr); |
| 94 | + |
| 95 | + for (String child : children.getOrDefault(curr, new ArrayList<>())) { |
| 96 | + q.offer(child); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + } |
| 102 | +} |
0 commit comments