-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk380.java
146 lines (127 loc) · 3.72 KB
/
wk380.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class wk380 {
// 遍历
public int maxFrequencyElements(int[] nums) {
int max = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
max = Math.max(max, map.get(num));
}
int ans = 0;
for (Integer value : map.values()) {
if (value == max) {
ans += value;
}
}
return ans;
}
// kmp+二分
public static List<Integer> findAll(String target, String pattern) {
int[] next = getNext(pattern);
int i = 0, j = 0;
List<Integer> result = new ArrayList<>();
while (i < target.length()) {
if (target.charAt(i) == pattern.charAt(j)) {
i++;
j++;
if (j == pattern.length()) {
result.add(i - j);
j = next[j];
}
} else {
j = next[j];
if (j == -1) {
i++;
j++;
}
}
}
return result;
}
private static int[] getNext(String pattern) {
int[] next = new int[pattern.length() + 1];
next[0] = -1;
int i = 0, j = -1;
while (i < pattern.length()) {
if (j == -1 || pattern.charAt(i) == pattern.charAt(j)) {
i++;
j++;
next[i] = j;
} else {
j = next[j];
}
}
return next;
}
TreeSet<Integer> help(String s, String a) {
TreeSet<Integer> l = new TreeSet<>();
for (Integer i : findAll(s, a)) {
l.add(i);
}
return l;
}
public List<Integer> beautifulIndices(String s, String a, String b, int k) {
TreeSet<Integer> al = help(s, a);
TreeSet<Integer> bl = help(s, b);
List<Integer> ans = new ArrayList<>();
for (Integer i : al) {
Integer ceiling = bl.ceiling(i - k);
if (ceiling != null && ceiling <= i + k) {
ans.add(i);
}
}
return ans;
}
// 二分+数位dp、找规律
public long findMaximumNumber(long k, int x) {
this.x = x;
long left = 0;
long right = (k + 1) << x;
//开区间二分
while (left + 1 < right) {
long mid = (left + right) >>> 1;
if (countDigitOne(mid) <= k) {
left = mid;
} else {
right = mid;
}
}
return left;
}
private int x;
private long num;
private long memo[][];
private long countDigitOne(long num) {
this.num = num;
int m = 64 - Long.numberOfLeadingZeros(num);
memo = new long[m][m + 1];
for (long[] row : memo) {
Arrays.fill(row, -1);
}
return dfs(m - 1, 0, true);
}
private long dfs(int i, int cnt1, boolean isLimit) {
if (i < 0) return cnt1;
if (!isLimit && memo[i][cnt1] != -1) return memo[i][cnt1];
int up = isLimit ? (int) (num >> i & 1) : 1;
long res = 0;
for (int d = 0; d <= up; d++) { // 枚举要填入的数字 d
res += dfs(i - 1, cnt1 + (d == 1 && (i + 1) % x == 0 ? 1 : 0), isLimit && d == up);
}
if (!isLimit) memo[i][cnt1] = res;
return res;
}
public static void main(String[] args) {
wk380 w = new wk380();
w.x = 1;
w.countDigitOne(10);
}
}