Repository navigation
Expand file tree
/
Copy pathLeetCode2418.java
More file actions
57 lines (53 loc) · 2.19 KB
/
Copy pathLeetCode2418.java
File metadata and controls
57 lines (53 loc) · 2.19 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
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class LeetCode2418 {
//22ms
public String[] sortPeople1(String[] names, int[] heights) {
return IntStream.range(0,heights.length)
.boxed()
.sorted(Comparator.<Integer>comparingInt(i->heights[i]).reversed()
.thenComparing(i->names[i],Comparator.naturalOrder()))
.map( i-> names[i])
.toList()
.toArray(new String[0]);
}
//23ms
public String[] sortPeople2(String[] names, int[] heights) {
return IntStream.range(0,heights.length)
.boxed()
.map( i -> Map.entry(names[i], heights[i]))
.sorted(Comparator.<Map.Entry<String,Integer>>comparingInt(Map.Entry::getValue).reversed()
.thenComparing(Map.Entry::getKey,Comparator.naturalOrder()))
.map( Map.Entry::getKey)
.toList()
.toArray(String[]::new);
}
//15ms
public String[] sortPeople(String[] names, int[] heights) {
// 1. Define a composite key that holds both fields
record Person(int height, String name) implements Comparable<Person> {
@Override
public int compareTo(Person other) {
// Height descending, then name ascending
int cmp = Integer.compare(other.height, this.height);
return cmp != 0 ? cmp : this.name.compareTo(other.name);
}
}
return IntStream.range(0, heights.length)
.boxed()
.collect(Collectors.toMap(
i -> new Person(heights[i], names[i]), // Key contains height + name
i -> heights[i], // Value
(ex, ne) -> ex,
TreeMap::new // Automatically sorts by Person's compareTo!
))
.keySet()
.stream()
.map( per -> per.name)
.toList()
.toArray(String[]::new);
}
}