-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETC.java
More file actions
95 lines (73 loc) · 2.37 KB
/
ETC.java
File metadata and controls
95 lines (73 loc) · 2.37 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
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
class Solution {
PriorityQueue<User> queue = new PriorityQueue<>();
public static void main(String[] args) {
Solution s = new Solution();
int k = 3;
String[] user_scores = {"zalex111 100", "cheries2 200", "coco 150", "luna 100", "zalex111 120", "coco 300", "cheries2 110", "zalex111 300"};
System.out.println(s.solution(k, user_scores));
}
public int solution(int k, String[] user_scores) {
AtomicInteger i = new AtomicInteger();
Arrays.stream(user_scores)
.map(score -> score.split(" "))
.forEach(e -> {
List<User> t1 = queue.stream()
.limit(k)
.collect(Collectors.toList());
updateUser(e[0], Integer.parseInt(e[1]));
List<User> t2 = queue.stream()
.limit(k)
.collect(Collectors.toList());
if (!t1.equals(t2)) {
i.getAndIncrement();
}
});
return i.get();
}
private void updateUser(String name, int score) {
User user = queue.stream()
.filter(u -> u.getName().equals(name))
.findFirst()
.orElse(null);
if (user != null) {
if (user.getScore() < score) {
queue.remove(user);
queue.offer(new User(name, score));
}
} else {
queue.offer(new User(name, score));
}
}
}
class User implements Comparable<User> {
private final String name;
private final int score;
public User(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
@Override
public int compareTo(User o) {
return o.score - this.score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return name.equals(user.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}