Skip to content

Commit 657e5fc

Browse files
committed
feat: leetcode contest 310
1 parent ae6b9e3 commit 657e5fc

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

acwing/算法基础课/第五讲 动态规划/线性DP/896. 最长上升子序列 II_RMQ解法.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
AcWing 3662 的简化版本
33
时间复杂度 O(n log n),不受数据大小影响
4+
https://www.acwing.com/solution/content/53104/
45
*/
56
#include <algorithm>
67
#include <cstring>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minGroups(vector<vector<int>>& ivs) {
4+
sort(ivs.begin(), ivs.end(), [](auto& a, auto& b) {
5+
return a[0] < b[0];
6+
});
7+
multiset<int> h;
8+
for (auto& iv : ivs) {
9+
int s = iv[0], e = iv[1];
10+
auto it = (h.lower_bound(s));
11+
if (!h.size() || it == h.begin()) {
12+
h.insert(e);
13+
} else {
14+
h.erase(--it);
15+
h.insert(e);
16+
}
17+
}
18+
return h.size();
19+
}
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const mostFrequentEven = function (nums) {
6+
const cnt = {}
7+
for (const x of nums) {
8+
cnt[x] ??= 0
9+
cnt[x]++
10+
}
11+
const ans = Object.entries(cnt).filter(x => x[0] % 2 === 0).sort((a, b) => {
12+
if (a[1] === b[1]) return Number(a[0]) - Number(b[0])
13+
return b[1] - a[1]
14+
})
15+
return ans[0]?.[0] ?? -1
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const partitionString = function (s) {
6+
const n = s.length
7+
let ans = 0
8+
for (let i = 0; i < n;) {
9+
ans++
10+
const c = new Set()
11+
let j = i
12+
while (j < n && !c.has(s[j])) c.add(s[j++])
13+
i = j
14+
}
15+
return ans
16+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const int N = 1e5 + 10;
2+
typedef long long LL;
3+
class Solution {
4+
int h[N] = {0}, a[N] = {0}, n;
5+
int lowbit(int x) {
6+
return x & (-x);
7+
}
8+
void updata(int x, int k) {
9+
while (x < N) {
10+
h[x] = k;
11+
int low = lowbit(x);
12+
for (int i = 1; i < low; i <<= 1)
13+
h[x] = max(h[x], h[x - i]);
14+
x += lowbit(x);
15+
}
16+
}
17+
int query(int x, int y) {
18+
int ans = 0;
19+
while (y >= x) {
20+
ans = max(a[y], ans), y -= 1;
21+
for (; y - lowbit(y) >= x; y -= lowbit(y))
22+
ans = max(h[y], ans);
23+
}
24+
return ans;
25+
}
26+
27+
public:
28+
int lengthOfLIS(vector<int>& nums, int k) {
29+
n = nums.size();
30+
// vector<int> dp(N, 0);
31+
32+
for (int i = 1; i <= n; i++) {
33+
int x = nums[i - 1];
34+
// int len = 1;
35+
// for (int j = max(1, a-k); j < a; j++) {
36+
// len = max(len, dp[j] + 1);
37+
// }
38+
int len = max(1, int(query(max(1, x - k), x - 1)) + 1);
39+
// dp[a] = max(dp[a], len);
40+
// int oldVal = ask(a, a);
41+
42+
// cout << x << " " << max(1, x-k) << " " << query(max(1, x-k), x-1) << " "<< len << endl;
43+
a[x] = len;
44+
updata(x, len);
45+
}
46+
int ans = 1;
47+
for (int i = 1; i <= n; i++) {
48+
int a = nums[i - 1];
49+
ans = max(ans, int(query(a, a)));
50+
}
51+
return ans;
52+
}
53+
};

template.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,34 @@ void add(int x, int y) {
646646
}
647647
```
648648

649+
```cpp
650+
// https://www.cnblogs.com/qdscwyy/p/9759220.html
651+
652+
int lowbit(int x) {
653+
return x & (-x);
654+
}
655+
void updata(int x, int k) {
656+
while (x <= n) {
657+
h[x] = k;
658+
int low = lowbit(x);
659+
for (int i = 1; i < low; i <<= 1)
660+
h[x] = max(h[x], h[x - i]);
661+
x += lowbit(x);
662+
}
663+
}
664+
// 区间查询 [x, y] 的 max
665+
int query(int x, int y) {
666+
int ans = 0;
667+
while (y >= x)
668+
{
669+
ans = max(a[y], ans), y -= 1;
670+
for (; y-lowbit(y) >= x; y -= lowbit(y))
671+
ans = max(h[y], ans);
672+
}
673+
return ans;
674+
}
675+
```
676+
649677
## 线段树
650678
651679
RMQ 常用算法,树状数组基于区间划分,线段树则是基于分治。

0 commit comments

Comments
 (0)