Skip to content

Commit 23914aa

Browse files
committed
weekly contest 390
1 parent 281f7d1 commit 23914aa

File tree

4 files changed

+346
-1
lines changed

4 files changed

+346
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ leetcode url: <https://leetcode.cn/u/cctest/>
66

77

88

9+
* 🐼 [weekly contest 390](src/main/java/weekly/wk390.java) 滑动窗口 | 枚举 | 懒删除 | 字典树
910
* 🐼 [weekly contest 387](src/main/java/weekly/wk387.java) 模拟 | 二维前缀和 | 模拟 | 树状数组
1011
* 🐼 [weekly contest 386](src/main/java/weekly/wk386.java) 遍历 | 贪心 | 二分 | 二分
1112
* 🐼 [weekly contest 382](src/main/java/weekly/wk382.java) 遍历 | 枚举 | 数学 | 贪心
@@ -92,7 +93,7 @@ leetcode url: <https://leetcode.cn/u/cctest/>
9293
* 🐶 [weekly contest 290](src/main/java/weekly/wk290.java)
9394
* 🐭 [weekly contest 280](src/main/java/weekly/wk289.java)
9495

95-
96+
* 🐸 [biweekly contest 126](src/main/java/weekly/wkb126.java) 模拟 | 堆、排序 | 堆、贪心 | DP(背包)
9697
* 🐸 [biweekly contest 125](src/main/java/weekly/wkb125.java) 遍历 | 堆 | 乘法原理 | 贪心、DP
9798
* 🐸 [biweekly contest 122](src/main/java/weekly/wkb122.java) 滑动窗口+双推 | 分组循环 | 贪心 | 滑动窗口+双堆
9899
* 🐸 [biweekly contest 121](src/main/java/weekly/wkb121.java) 遍历 | 贪心 | 异或 | 数位dp

src/main/java/weekly/wk389.java

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
public class wk389 {
10+
11+
// 模拟
12+
public boolean isSubstringPresent(String s) {
13+
Set<String> list = new HashSet<>();
14+
for (int i = 0; i < s.length() - 1; i++) {
15+
list.add(s.substring(i, i + 2));
16+
}
17+
String sb = new StringBuilder(s).reverse().toString();
18+
for (int i = 0; i < sb.length() - 1; i++) {
19+
if (list.contains(sb.substring(i, i + 2))) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
25+
}
26+
27+
28+
//前缀和 数学
29+
public long countSubstrings(String s, char c) {
30+
int[] count = new int[s.length() + 1];
31+
for (int i = 0; i < s.length(); i++) {
32+
count[i + 1] = count[i];
33+
if (s.charAt(i) == c) {
34+
count[i + 1]++;
35+
}
36+
}
37+
38+
long ans = 0;
39+
for (int i = 0; i < s.length(); i++) {
40+
if (s.charAt(i) == c) {
41+
int left = count[i];
42+
int right = count[s.length()] - left;
43+
ans += right;
44+
}
45+
46+
}
47+
return ans;
48+
}
49+
50+
//
51+
static public int minimumDeletions(String word, int k) {
52+
int[] count = new int[26];
53+
for (int i = 0; i < word.length(); i++) {
54+
count[word.charAt(i) - 'a']++;
55+
}
56+
57+
Arrays.sort(count);
58+
int[] pre = new int[count.length + 1];
59+
for (int i = 0; i < count.length; i++) {
60+
pre[i + 1] = count[i] + pre[i];
61+
}
62+
int i = 0;
63+
while (count[i] == 0) {
64+
i++;
65+
}
66+
int ans = Integer.MAX_VALUE;
67+
for (; i < count.length; i++) {
68+
if (i > 0 && count[i] == count[i - 1]) continue;
69+
int del = 0;
70+
for (int j = i + 1; j < count.length; j++) {
71+
if (count[j] - count[i] > k) {
72+
del += count[j] - count[i] - k;
73+
}
74+
}
75+
del += pre[i];
76+
ans = Math.min(ans, del);
77+
}
78+
return ans;
79+
}
80+
81+
public static void main(String[] args) {
82+
minimumDeletions("aabcaba",0);
83+
}
84+
}

src/main/java/weekly/wk390.java

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package weekly;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
import java.util.TreeMap;
7+
8+
public class wk390 {
9+
//滑动窗口
10+
public int maximumLengthSubstring(String s) {
11+
int[] count = new int[26];
12+
int left = 0;
13+
int ans = 0;
14+
for (int i = 0; i < s.length(); i++) {
15+
char c = s.charAt(i);
16+
count[c - 'a']++;
17+
18+
while (count[c - 'a'] > 2) {
19+
count[s.charAt(left++) - 'a']--;
20+
}
21+
ans = Math.max(ans, i - left + 1);
22+
}
23+
return ans;
24+
}
25+
26+
//枚举k
27+
public int minOperations(int k) {
28+
int ans = k - 1;
29+
for (int i = 1; i <= k; i++) {
30+
int tmp = (i - 1);
31+
tmp += (k / i - 1) + (k % i > 0 ? 1 : 0);
32+
ans = Math.min(ans, tmp);
33+
}
34+
return ans;
35+
}
36+
37+
38+
//懒删除
39+
static public long[] mostFrequentIDs(int[] nums, int[] freq) {
40+
Map<Long, Long> map = new HashMap<>();
41+
PriorityQueue<long[]> priorityQueue = new PriorityQueue<>((a, b) -> -Long.compare(a[0], b[0]));
42+
long[] ans = new long[nums.length];
43+
for (int i = 0; i < nums.length; i++) {
44+
long num = nums[i];
45+
Long count = map.getOrDefault(num, 0L) + freq[i];
46+
map.put(num, count);
47+
priorityQueue.add(new long[]{count, num});
48+
49+
while (map.get(priorityQueue.peek()[1]) != priorityQueue.peek()[0]) {
50+
priorityQueue.poll();
51+
}
52+
ans[i] = priorityQueue.peek()[0];
53+
}
54+
return ans;
55+
}
56+
57+
58+
//字典树
59+
public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
60+
Trie t = new Trie();
61+
for (int i = 0; i < wordsContainer.length; i++) {
62+
t.insert(wordsContainer[i], i);
63+
}
64+
int[] ans = new int[wordsQuery.length];
65+
for (int i = 0; i < wordsQuery.length; i++) {
66+
ans[i] = t.search(wordsQuery[i]);
67+
}
68+
return ans;
69+
}
70+
71+
class Trie {
72+
Trie[] children;
73+
int maxIndex = -1;
74+
int minLen = Integer.MAX_VALUE;
75+
76+
public Trie() {
77+
children = new Trie[26];
78+
}
79+
80+
public void insert(String word, int wIndex) {
81+
Trie cur = this;
82+
if (word.length() < cur.minLen) {
83+
cur.minLen = word.length();
84+
cur.maxIndex = wIndex;
85+
}
86+
Trie[] branches = cur.children;
87+
char[] chs = word.toCharArray();
88+
for (int i = chs.length - 1; i >= 0; i--) {
89+
int index = chs[i] - 'a';
90+
if (branches[index] == null) {
91+
branches[index] = new Trie();
92+
}
93+
cur = branches[index];
94+
branches = cur.children;
95+
if (word.length() < cur.minLen) {
96+
cur.minLen = word.length();
97+
cur.maxIndex = wIndex;
98+
}
99+
}
100+
}
101+
102+
public int search(String word) {
103+
Trie cur = this;
104+
Trie[] branches = children;
105+
char[] chs = word.toCharArray();
106+
for (int i = chs.length - 1; i >= 0; i--) {
107+
int index = chs[i] - 'a';
108+
if (branches[index] != null) {
109+
cur = branches[index];
110+
branches = cur.children;
111+
} else {
112+
return cur.maxIndex;
113+
}
114+
}
115+
return cur.maxIndex;
116+
}
117+
}
118+
119+
120+
public static void main(String[] args) {
121+
mostFrequentIDs(new int[]{5, 5, 3}, new int[]{2, -2, 1});
122+
}
123+
}

src/main/java/weekly/wkb126.java

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
import java.util.Set;
10+
11+
public class wkb126 {
12+
//模拟
13+
public int sumOfEncryptedInt(int[] nums) {
14+
15+
int ans = 0;
16+
for (int num : nums) {
17+
String s = num + "";
18+
int max = 0;
19+
for (int i = 0; i < s.length(); i++) {
20+
int c = s.charAt(i) - '0';
21+
max = Math.max(max, c);
22+
}
23+
24+
25+
int n = 0;
26+
for (int i = 0; i < s.length(); i++) {
27+
n = n * 10 + max;
28+
}
29+
ans += n;
30+
}
31+
return ans;
32+
}
33+
34+
35+
//排序or小根堆
36+
public long[] unmarkedSumArray(int[] nums, int[][] queries) {
37+
38+
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
39+
40+
long sum = 0;
41+
for (int i = 0; i < nums.length; i++) {
42+
sum += nums[i];
43+
priorityQueue.add(new int[]{nums[i], i});
44+
}
45+
46+
Set<Integer> set = new HashSet<>();
47+
long[] ans = new long[queries.length];
48+
49+
for (int j = 0; j < queries.length; j++) {
50+
int[] query = queries[j];
51+
int index = query[0];
52+
int k = query[1];
53+
if (!set.contains(index)) {
54+
sum -= nums[index];
55+
set.add(index);
56+
}
57+
for (int i = 0; i < k; i++) {
58+
if (priorityQueue.isEmpty()) break;
59+
int[] poll = priorityQueue.poll();
60+
if (!set.contains(poll[1])) {
61+
sum -= poll[0];
62+
set.add(poll[1]);
63+
} else {
64+
i--;
65+
}
66+
}
67+
ans[j] = sum;
68+
}
69+
return ans;
70+
}
71+
72+
73+
74+
// 小根堆/贪心
75+
public String minimizeStringValue(String s) {
76+
77+
int need = 0;
78+
int[] count = new int[26];
79+
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
80+
Arrays.fill(count, 0);
81+
for (int i = 0; i < s.length(); i++) {
82+
if (s.charAt(i) == '?') {
83+
need++;
84+
} else {
85+
count[s.charAt(i) - 'a']++;
86+
}
87+
}
88+
for (int i = 0; i < count.length; i++) {
89+
priorityQueue.add(new int[]{count[i], i});
90+
}
91+
92+
List<Character> list = new ArrayList<>();
93+
for (int i = 0; i < need; i++) {
94+
int[] poll = priorityQueue.poll();
95+
list.add((char) ('a' + poll[1]));
96+
poll[0]++;
97+
priorityQueue.add(poll);
98+
}
99+
100+
Collections.sort(list);
101+
102+
int index = 0;
103+
StringBuilder sb = new StringBuilder();
104+
for (int i = 0; i < s.length(); i++) {
105+
if (s.charAt(i) == '?') {
106+
sb.append(list.get(index++));
107+
} else {
108+
sb.append(s.charAt(i));
109+
}
110+
}
111+
return sb.toString();
112+
}
113+
114+
115+
//dp
116+
public int sumOfPower(int[] nums, int k) {
117+
int mod = (int) 1e9 + 7;
118+
int[] mem = new int[k + 1];
119+
mem[0] = 1;
120+
for (int num : nums) {
121+
int[] nextMem = new int[k + 1];
122+
for (int j = k; j >= 0; --j) {
123+
// 子序列累计,加入or不加入两种
124+
if (j - num >= 0) {
125+
nextMem[j] = ((2 * mem[j]) % mod + mem[j - num]) % mod;
126+
} else {
127+
nextMem[j] = (2 * mem[j]) % mod;
128+
}
129+
}
130+
mem = nextMem;
131+
}
132+
return mem[k];
133+
}
134+
135+
public static void main(String[] args) {
136+
}
137+
}

0 commit comments

Comments
 (0)