-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk390.java
123 lines (108 loc) · 3.56 KB
/
wk390.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package weekly;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.TreeMap;
public class wk390 {
//滑动窗口
public int maximumLengthSubstring(String s) {
int[] count = new int[26];
int left = 0;
int ans = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
count[c - 'a']++;
while (count[c - 'a'] > 2) {
count[s.charAt(left++) - 'a']--;
}
ans = Math.max(ans, i - left + 1);
}
return ans;
}
//枚举k
public int minOperations(int k) {
int ans = k - 1;
for (int i = 1; i <= k; i++) {
int tmp = (i - 1);
tmp += (k / i - 1) + (k % i > 0 ? 1 : 0);
ans = Math.min(ans, tmp);
}
return ans;
}
//懒删除
static public long[] mostFrequentIDs(int[] nums, int[] freq) {
Map<Long, Long> map = new HashMap<>();
PriorityQueue<long[]> priorityQueue = new PriorityQueue<>((a, b) -> -Long.compare(a[0], b[0]));
long[] ans = new long[nums.length];
for (int i = 0; i < nums.length; i++) {
long num = nums[i];
Long count = map.getOrDefault(num, 0L) + freq[i];
map.put(num, count);
priorityQueue.add(new long[]{count, num});
while (map.get(priorityQueue.peek()[1]) != priorityQueue.peek()[0]) {
priorityQueue.poll();
}
ans[i] = priorityQueue.peek()[0];
}
return ans;
}
//字典树
public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
Trie t = new Trie();
for (int i = 0; i < wordsContainer.length; i++) {
t.insert(wordsContainer[i], i);
}
int[] ans = new int[wordsQuery.length];
for (int i = 0; i < wordsQuery.length; i++) {
ans[i] = t.search(wordsQuery[i]);
}
return ans;
}
class Trie {
Trie[] children;
int maxIndex = -1;
int minLen = Integer.MAX_VALUE;
public Trie() {
children = new Trie[26];
}
public void insert(String word, int wIndex) {
Trie cur = this;
if (word.length() < cur.minLen) {
cur.minLen = word.length();
cur.maxIndex = wIndex;
}
Trie[] branches = cur.children;
char[] chs = word.toCharArray();
for (int i = chs.length - 1; i >= 0; i--) {
int index = chs[i] - 'a';
if (branches[index] == null) {
branches[index] = new Trie();
}
cur = branches[index];
branches = cur.children;
if (word.length() < cur.minLen) {
cur.minLen = word.length();
cur.maxIndex = wIndex;
}
}
}
public int search(String word) {
Trie cur = this;
Trie[] branches = children;
char[] chs = word.toCharArray();
for (int i = chs.length - 1; i >= 0; i--) {
int index = chs[i] - 'a';
if (branches[index] != null) {
cur = branches[index];
branches = cur.children;
} else {
return cur.maxIndex;
}
}
return cur.maxIndex;
}
}
public static void main(String[] args) {
mostFrequentIDs(new int[]{5, 5, 3}, new int[]{2, -2, 1});
}
}