Skip to content

Commit f6c60ad

Browse files
committed
feat: leetcode contest 293
1 parent df2b931 commit f6c60ad

6 files changed

+221
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {string[]}
4+
*/
5+
const removeAnagrams = function (words) {
6+
const ans = []
7+
let last
8+
for (let i = 0; i < words.length; i++) {
9+
cur = words[i].split('').sort().join('')
10+
if (cur === last) continue
11+
else {
12+
ans.push(words[i])
13+
last = cur
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} bottom
3+
* @param {number} top
4+
* @param {number[]} special
5+
* @return {number}
6+
*/
7+
const maxConsecutive = function (b, t, s) {
8+
s.sort((a, b) => a - b)
9+
s.push(1e10)
10+
const n = s.length
11+
let ans = 0
12+
for (let i = 0; i < n; i++) {
13+
if (s[i] <= b) continue
14+
if (s[i] > t) {
15+
ans = Math.max(ans, t - Math.max(s[i - 1] + 1, b) + 1)
16+
break
17+
}
18+
19+
let last = b
20+
if (i > 0) last = Math.max(s[i - 1] + 1, b)
21+
const len = s[i] - last
22+
ans = Math.max(ans, len)
23+
// console.log(i, last, s[i], len, ans)
24+
}
25+
return ans
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @return {number}
4+
*/
5+
const largestCombination = function (cs) {
6+
let ans = 0
7+
for (let i = 31; i >= 0; i--) {
8+
let cnt = 0
9+
for (const s of cs) {
10+
if (s >> i & 1) cnt++
11+
}
12+
ans = Math.max(ans, cnt)
13+
}
14+
return ans
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CountIntervals {
2+
CountIntervals *left = nullptr, *right = nullptr;
3+
int l, r, sum = 0;
4+
5+
public:
6+
CountIntervals() : l(1), r(1e9) {}
7+
8+
CountIntervals(int l, int r) : l(l), r(r) {}
9+
10+
void add(int L, int R) { // 为方便区分变量名,将递归中始终不变的入参改为大写(视作常量)
11+
if (sum == r - l + 1) return; // 当前节点已被完整覆盖,无需执行任何操作
12+
if (L <= l && r <= R) { // 当前节点已被区间 [L,R] 完整覆盖,不再继续递归
13+
sum = r - l + 1;
14+
return;
15+
}
16+
int mid = (l + r) / 2;
17+
if (left == nullptr) left = new CountIntervals(l, mid); // 动态开点
18+
if (right == nullptr) right = new CountIntervals(mid + 1, r); // 动态开点
19+
if (L <= mid) left->add(L, R);
20+
if (mid < R) right->add(L, R);
21+
sum = left->sum + right->sum;
22+
}
23+
24+
int count() { return sum; }
25+
};
26+
27+
// 作者:endlesscheng
28+
// 链接:https://leetcode.cn/problems/count-integers-in-intervals/solution/by-endlesscheng-clk2/
29+
// 来源:力扣(LeetCode)
30+
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class CountIntervals {
5+
typedef pair<int, int> pii;
6+
int ans = 0;
7+
set<pii> st;
8+
9+
public:
10+
CountIntervals() {
11+
}
12+
13+
void add(int left, int right) {
14+
// st 中已有的区间总是保持互不相交的
15+
auto it = st.lower_bound({left - 1, -1});
16+
while (it != st.end()) {
17+
// 不可能合并了
18+
if (it->second >= right + 1) break;
19+
right = max(right, it->first);
20+
left = min(left, it->second);
21+
ans -= it->first - it->second + 1;
22+
st.erase(it++);
23+
}
24+
ans += right - left + 1;
25+
st.insert({right, left});
26+
}
27+
28+
int count() {
29+
return ans;
30+
}
31+
};
32+
33+
/**
34+
* Your CountIntervals object will be instantiated and called as such:
35+
* CountIntervals* obj = new CountIntervals();
36+
* obj->add(left,right);
37+
* int param_2 = obj->count();
38+
*/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
function Node (start, end) {
2+
this.val = 0
3+
this.start = start
4+
this.end = end
5+
this.left = null
6+
this.right = null
7+
}
8+
9+
function SegmentTree (start, end) {
10+
this.root = new Node(start, end)
11+
}
12+
13+
SegmentTree.prototype.update = function (start, end) {
14+
this.updateNode(this.root, start, end)
15+
}
16+
17+
SegmentTree.prototype.updateNode = function (node, start, end) {
18+
if (!node) {
19+
return
20+
}
21+
if (start > node.end || end < node.start) {
22+
23+
} else if (start <= node.start && end >= node.end) {
24+
node.val = node.end - node.start + 1
25+
} else {
26+
this.pushdown(node)
27+
this.updateNode(node.left, start, end)
28+
this.updateNode(node.right, start, end)
29+
this.pushup(node)
30+
}
31+
}
32+
33+
SegmentTree.prototype.pushdown = function (node) {
34+
if (!node) {
35+
return
36+
}
37+
const mid = Math.floor((node.start + node.end) / 2)
38+
if (!node.left) {
39+
node.left = new Node(node.start, mid)
40+
}
41+
if (!node.right) {
42+
node.right = new Node(mid + 1, node.end)
43+
}
44+
if (node.val === (node.end - node.start + 1)) {
45+
node.left.val = mid - node.start + 1
46+
node.right.val = node.end - mid
47+
}
48+
}
49+
50+
SegmentTree.prototype.pushup = function (node) {
51+
node.val = node.left.val + node.right.val
52+
}
53+
54+
SegmentTree.prototype.query = function (start, end) {
55+
return this.queryNode(this.root, start, end)
56+
}
57+
58+
SegmentTree.prototype.queryNode = function (node, start, end) {
59+
if (!node) {
60+
return 0
61+
}
62+
if (start > node.end || end < node.start) {
63+
return 0
64+
} else if (start <= node.start && end >= node.end) {
65+
return node.val
66+
} else {
67+
this.pushdown(node)
68+
return this.queryNode(node.left, start, end) + this.queryNode(node.right, start, end)
69+
}
70+
}
71+
72+
const CountIntervals = function () {
73+
this.tree = new SegmentTree(0, 1e9)
74+
}
75+
76+
/**
77+
* @param {number} left
78+
* @param {number} right
79+
* @return {void}
80+
*/
81+
CountIntervals.prototype.add = function (left, right) {
82+
this.tree.update(left, right)
83+
}
84+
85+
/**
86+
* @return {number}
87+
*/
88+
CountIntervals.prototype.count = function () {
89+
return this.tree.query(0, 1e9)
90+
}
91+
92+
// 作者:scnu_evan
93+
// 链接:https://leetcode.cn/circle/discuss/h00hT2/
94+
// 来源:力扣(LeetCode)
95+
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 commit comments

Comments
 (0)