-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk293.java
211 lines (171 loc) · 6.31 KB
/
wk293.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class wk293 {
//ranking: 1746 / 7357
//简单题,统计字母个数比较就可以了
public List<String> removeAnagrams(String[] words) {
List<String> res = new ArrayList<>();
String pre = "";
for (int i = 0; i < words.length; i++) {
int[] count = new int[26];
for (int j = 0; j < words[i].length(); j++) {
int c = words[i].charAt(j) - 'a';
count[c]++;
}
String s = "";
for (int j : count) {
s += j;
}
if (!pre.equals(s)) res.add(words[i]);
pre = s;
}
return res;
}
//中等题,对special排序,两个sp之间的距离就是连续楼层,求其最大值,注意上下边界
static public int maxConsecutive(int bottom, int top, int[] special) {
int pre = bottom;
int max = 0;
Arrays.sort(special);
for (int i = 0; i < special.length; i++) {
max = Math.max(max, special[i] - pre);
pre = special[i] + 1;
}
max = Math.max(max, top - pre + 1);
return max;
}
//中等题,灵活转换,&操作下,只有都是1的位置才会结果为1,统计二进制下每个位置1的个数即可
public int largestCombination(int[] candidates) {
int[] count = new int[32];
for (int i = 0; i < candidates.length; i++) {
int num = candidates[i];
for (int j = 0; j < 32; j++) {
if (((num >> j) & 1) > 0) {
count[j]++;
}
}
}
int max = 0;
for (int i = 0; i < count.length; i++) {
max = Math.max(count[i], max);
}
return max;
}
/* static class CountIntervals {
//start end;
TreeMap<Integer, Integer> tree = new TreeMap<>();
int count = 0;
public CountIntervals() {
}
int sum=0;
public void add(int left, int right) {
System.out.println("s"+ left+ " "+right);
//已经被覆盖了
Map.Entry<Integer, Integer> over = tree.floorEntry(left);
if (over != null && over.getValue() >= right) return;
count += (right - left + 1);
//先查看是否覆盖了区间
while (tree.ceilingEntry(left) != null) {
Map.Entry<Integer, Integer> high = tree.ceilingEntry(left);
if (high.getValue() <= right) {
count -= (high.getValue() - high.getKey() + 1);
tree.remove(high.getKey());
} else break;
}
boolean flag=false;
//左侧是否重叠
Map.Entry<Integer, Integer> leftNode = tree.floorEntry(left);
if (leftNode != null && leftNode.getValue() <= right && leftNode.getValue() >= left) {
System.out.println("left");
tree.put(leftNode.getKey(), right);
count -= (leftNode.getValue() - left + 1);
flag=true;
System.out.println(count);
}
//右侧是否重叠
Map.Entry<Integer, Integer> rightNode = tree.floorEntry(right);
if (rightNode != null && rightNode.getKey() >= left && rightNode.getKey() <= right) {
System.out.println("right");
tree.remove(rightNode.getKey());
tree.put(left, rightNode.getValue());
count -= (right - rightNode.getKey() + 1);
flag=true;
System.out.println(count);
}
//未覆盖
if(!flag) tree.put(left,right);
System.out.println(sum+++" "+count);
}
public int count() {
return count;
}
}*/
//困难题,合并区间,之前做过类似的题目,这次又没做出来,XXXX
//treeMap保存区间的头尾,当有闲的区间进来的时候,不断的找到<=right的位置,并且要>=left,删除此区间,更新心的区间大小
//线段树:动态开点线段树,防止范围太大 out of memory
/* class CountIntervals {
TreeMap<Integer, Integer> map = new TreeMap<>();
int ans = 0;
public CountIntervals() {
}
public void add(int left, int right) {
Integer L = map.floorKey(right);
int l = left, r = right;
while (L != null && map.get(L) >= l) { //判断有交集
l = Math.min(l, L); //求最小左侧
r = Math.max(r, map.get(L)); //求最大右侧
ans -= (map.get(L) - L + 1);//count也要删掉
map.remove(L); //删掉此段
L = map.floorKey(right);
}
ans += (r - l + 1);
map.put(l, r);
}
public int count() {
return ans;
}
}*/
//线段树:动态开点线段树,防止范围太大 out of memory
class CountIntervals {
int l, r, cnt;
CountIntervals L, R;
public CountIntervals() {
this.l = 1;
this.r = (int) 1e9;
}
public CountIntervals(int l, int r) {
this.l = l;
this.r = r;
}
public void add(int left, int right) {
//表示这一段之前有过,再加还是这样, 直接返回
if (cnt == r - l + 1) return;
//表示完全覆盖了这段 直接返回
if (left <= l && right >= r) {
cnt = r - l + 1;
return;
}
//均分这一段
int mid = (l + r) / 2;
if (L == null) L = new CountIntervals(l, mid);
if (R == null) R = new CountIntervals(mid + 1, r);
//和左边有没有交集
if (left <= mid) L.add(left, right);
//和右边有没有交集
if (mid < right) R.add(left, right);
cnt = L.cnt + R.cnt;
}
public int count() {
return cnt;
}
}
public static void main(String[] args) {
/* CountIntervals c=new CountIntervals();
c.add(2,3);
c.add(7,10);
c.count();
c.add(5,8);
c.count();*/
}
}