-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitsort.java
47 lines (39 loc) · 1.2 KB
/
bitsort.java
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
import java.util.*;
public class bitsort {
public static List<Integer> sortByBits(List<Integer> arr) {
int n = arr.size();
for (int i = 0; i < n; i++)
arr.set(i, arr.get(i) + 10001 * Integer.bitCount(arr.get(i)));
Collections.sort(arr);
for (int i = 0; i < n; i++)
arr.set(i, arr.get(i) % 10001);
return arr;
}
public static int longestStrChain(List<String> words) {
int ans = 0;
Map<String, Integer> dp = new HashMap<>();
Collections.sort(words, (a, b) -> a.length() - b.length());
for (final String word : words) {
int bestLength = 0;
for (int i = 0; i < word.length(); ++i) {
final String pred = word.substring(0, i) + word.substring(i + 1);
bestLength = Math.max(bestLength, dp.getOrDefault(pred, 0) + 1);
}
dp.put(word, bestLength);
ans = Math.max(ans, bestLength);
}
return ans;
}
// ["a","b","ba","bca","bda","bdca"]
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("ba");
list.add("bca");
list.add("bda");
list.add("bdca");
int res = longestStrChain(list);
System.out.println(res);
}
}