周赛293

This commit is contained in:
轩辕龙儿 2022-05-15 21:01:46 +08:00
parent 20b0208eb9
commit adcff79089
2 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package contest.y2022.m5.week;
import java.util.Iterator;
import java.util.TreeSet;
/**
* leet-code
*
* @author 轩辕龙儿
* @date 2022/5/15 11:41
*/
public class CountIntervals {
TreeSet<Interval> ranges;
int cnt;
public CountIntervals() {
ranges = new TreeSet();
cnt = 0;
}
public void add(int left, int right) {
Iterator<Interval> itr = ranges.tailSet(new Interval(0, left - 1)).iterator();
while (itr.hasNext()) {
Interval iv = itr.next();
if (right < iv.left) break;
left = Math.min(left, iv.left);
right = Math.max(right, iv.right);
cnt -= iv.right - iv.left + 1;
itr.remove();
}
ranges.add(new Interval(left, right));
cnt += right - left + 1;
}
public int count() {
return cnt;
}
}
class Interval implements Comparable<Interval> {
int left;
int right;
public Interval(int left, int right) {
this.left = left;
this.right = right;
}
public int compareTo(Interval that) {
if (this.right == that.right) return this.left - that.left;
return this.right - that.right;
}
}

View File

@ -0,0 +1,79 @@
package contest.y2022.m5.week;
import java.util.*;
/**
* leet-code
*
* @author 轩辕龙儿
* @date 2022/5/15 10:29
*/
public class Solution293 {
public static void main(String[] args) {
Solution293 solution = new Solution293();
// solution.maxConsecutive(6, 8, new int[]{7, 6, 8});
CountIntervals ci = new CountIntervals();
ci.add(2,3);
ci.add(7,10);
System.out.println(ci.cnt);
ci.add(5,8);
System.out.println(ci.cnt);
}
public List<String> removeAnagrams(String[] words) {
char[] strs = words[0].toCharArray();
Arrays.sort(strs);
List<String> list = new ArrayList<>();
int index = 0;
for (int i = 1; i < words.length; i++) {
char[] strs1 = words[i].toCharArray();
Arrays.sort(strs1);
if (!String.valueOf(strs).equals(String.valueOf(strs1))) {
list.add(words[index]);
strs = strs1;
index = i;
}
}
list.add(words[index]);
return list;
}
public int maxConsecutive(int bottom, int top, int[] special) {
Arrays.sort(special);
int max = 0;
for (int i = 0; i < special.length; i++) {
if (special[i] < bottom) {
continue;
}
if (special[i] > top) {
if (i > 0) {
max = Math.max(max, top - special[i - 1]);
}
break;
}
if (bottom <= special[i]) {
max = Math.max(max, special[i] - bottom);
bottom = special[i] + 1;
}
}
if (top > special[special.length - 1]) {
max = Math.max(max, top - special[special.length - 1]);
}
return max;
}
public int largestCombination(int[] candidates) {
int max = 0;
for (int i = 0; i < 25; i++) {
int cnt = 0;
for (int j = 0; j < candidates.length; j++) {
if ((candidates[j] & (1 << i)) > 0) {
cnt++;
}
}
max = Math.max(max, cnt);
}
return max;
}
}