forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoteProblem.java
More file actions
82 lines (69 loc) · 2.3 KB
/
VoteProblem.java
File metadata and controls
82 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.*;
public class VoteProblem {
public static void main(String[] args) {
List<Vote> ls = new ArrayList<>();
ls.add(new Vote("a", 100)); // leader = {"a"}, 100 -> "a"
ls.add(new Vote("b", 150)); // leader = {"a", "b"}, 150 -> ("a", "b")
ls.add(new Vote("b", 180)); // leader = {"b"}, 180 -> ("b")
ls.add(new Vote("a", 180));
ls.add(new Vote("a", 200)); // leader = {"a", "b"}, 200 -> ("a", "b")
Map<String, Integer> counts = new HashMap<> ();
int maxVotes = 0;
Set<String> leaders = new HashSet<> ();
List<Node> timeline = new ArrayList<> ();
for(Vote vote: ls) {
int ts = vote.ts;
String cand = vote.cand;
counts.put(cand, counts.getOrDefault(cand, 0) + 1);
if(counts.getOrDefault(cand, 0) == maxVotes) {
leaders.add(cand);
} else if(counts.get(cand) > maxVotes) {
maxVotes = counts.get(cand);
leaders.clear();
leaders.add(cand);
}
// System.out.println(leaders);
timeline.add(new Node(new HashSet<>(leaders), ts));
}
for(Node node: timeline) {
System.out.println(node.ts + "\t" + node.cands);
}
Set<String> winners = findLeadersSofar(timeline, 180);
System.out.println("winners are: " + winners);
}
static Set<String> findLeadersSofar(List<Node> timeline, int T) {
int n = timeline.size();
if(n == 0) return new HashSet<> ();
int l = 0, r = n-1, rid = -1;
while(l <= r) {
int mptr = (l + r) / 2;
Node mid = timeline.get(mptr);
if(mid.ts > T) {
r = mptr - 1;
} else if(mid.ts < T) {
rid = mptr;
l = mptr + 1;
} else {
rid = mptr;
l += 1;
}
}
return timeline.get(rid).cands;
}
static class Vote {
int ts;
String cand;
public Vote(String ca, int t) {
cand = ca;
ts = t;
}
}
static class Node {
int ts;
Set<String> cands;
public Node(Set<String> c, int t) {
cands = c;
ts = t;
}
}
}