枚举:枚举子数组,用集合记录当前子数组中不同元素的个数
class Solution {
public:
using ll = long long;
int sumCounts(vector<int> &nums) {
ll mod = 1e9 + 7;
int n = nums.size();
unordered_set<int> s;
ll res = 0;
for (int l = 0; l < n; l++) {
for (int r = l; r < n; r++) {//子数组nums[l,r]
s.insert(nums[r]);
res += s.size() * s.size();
res %= mod;
}
s.clear();
}
return (res + mod) % mod;
}
};
动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] 为将 s [ 0 , i − 1 ] s[0,i-1] s[0,i−1] 修改为末尾为 j j j 的美丽字符串的最少修改次数, p [ i ] [ j ] p[i][j] p[i][j] 可由 p [ i − 2 ] [ 0 ] p[i-2][0] p[i−2][0] 和 p [ i − 2 ] [ 1 ] p[i-2][1] p[i−2][1] 转移得到
class Solution {
public:
int minChanges(string s) {
int n = s.size();
int p[n + 1][2];
p[0][0] = 0;
p[0][1] = 0;
for (int i = 2; i <= n; i += 2) {
p[i][0] = min(p[i - 2][0], p[i - 2][1]) + (s[i - 2] == '0' ? 0 : 1) + (s[i - 1] == '0' ? 0 : 1);
p[i][1] = min(p[i - 2][0], p[i - 2][1]) + (s[i - 2] == '1' ? 0 : 1) + (s[i - 1] == '1' ? 0 : 1);
}
return min(p[n][0], p[n][1]);
}
};
动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] 为 n u m s [ 0 , i − 1 ] nums[0,i-1] nums[0,i−1] 的和为 j j j 的子序列的最长长度,有状态转移方程: p [ i + 1 ] [ j ] = m i n { p [ i ] [ j ] , p [ i ] [ j − n u m s [ i ] ] + 1 } p[i+1][j]=min\{p[i][j], p[i][j-nums[i]]+1 \} p[i+1][j]=min{p[i][j],p[i][j−nums[i]]+1}
class Solution {
public:
int lengthOfLongestSubsequence(vector<int> &nums, int target) {
int n = nums.size();
int p[n + 1][target + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= target; j++)
p[i][j] = -1;//初始化为无效状态
p[0][0] = 0;//空序列
for (int i = 0; i < n; i++) {
for (int j = 0; j <= target; j++) {
p[i + 1][j] = p[i][j];
if (j - nums[i] >= 0 && p[i][j - nums[i]] != -1)
p[i + 1][j] = max(p[i + 1][j], p[i][j - nums[i]] + 1);
}
}
return p[n][target];
}
};
线段树:遍历数组 n u m s [ i ] nums[i] nums[i] ,设 c [ j ] c[j] c[j] 为子数组 n u m s [ j , i ] nums[j,i] nums[j,i] 中不同元素数目,考虑子数组右端点从 i − 1 i-1 i−1 移动到 i i i 时 c c c 数组的变化:
- 若 n u m s [ i ] nums[i] nums[i] 为该元素第一次出现,则 c c c 数组在 [ 0 , i ] [0,i] [0,i] 区间的元素 + 1 +1 +1
- 若 n u m s [ i ] nums[i] nums[i] 不是该元素第一次出现,设最近的一次出现为位置为 l e f t [ i ] − 1 left[i]-1 left[i]−1,则 c c c 数组在 [ l e f t [ i ] , i ] [left[i],i] [left[i],i] 区间的元素 + 1 +1 +1
可以通过实现支持 区间加 和 查询区间平方和 的线段树来求解,因为 ( a l + d ) 2 + ⋯ + ( a r + d ) 2 = ( a l 2 + ⋯ + a r 2 ) + 2 × d × ( a l + ⋯ + a r ) + d 2 × ( r − l + 1 ) (a_l+d)^2+\cdots +(a_r+d)^2 =(a_l^2+\cdots +a_r^2)+2\times d\times(a_l+\cdots +a_r)+d^2\times (r-l+1) (al+d)2+⋯+(ar+d)2=(al2+⋯+ar2)+2×d×(al+⋯+ar)+d2×(r−l+1),所以线段树内部可以通过维护区间和和区间平方和来实现。
class SegmentTree {
public:
typedef long long ll;
inline void push_down(ll index) {
st[index << 1].lazy = 1;
st[index << 1 | 1].lazy = 1;
st[index << 1].mark += st[index].mark;
st[index << 1 | 1].mark += st[index].mark;
st[index << 1].s2 += st[index << 1].s * st[index].mark * 2 + (st[index << 1].tr - st[index << 1].tl + 1) * st[index].mark * st[index].mark;
st[index << 1].s += st[index].mark * (st[index << 1].tr - st[index << 1].tl + 1);
st[index << 1 | 1].s2 += st[index << 1 | 1].s * st[index].mark * 2 + (st[index << 1 | 1].tr - st[index << 1 | 1].tl + 1) * st[index].mark * st[index].mark;
st[index << 1 | 1].s += st[index].mark * (st[index << 1 | 1].tr - st[index << 1 | 1].tl + 1);
st[index].lazy = 0;
st[index].mark = 0;
}
inline void push_up(ll index) {
st[index].s = st[index << 1].s + st[index << 1 | 1].s;
st[index].s2 = st[index << 1].s2 + st[index << 1 | 1].s2;
}
SegmentTree(int n) {
st = vector<SegmentTreeNode>(n * 4 + 10);
build(1, n);
}
void build(ll l, ll r, ll index = 1) {
st[index].tl = l;
st[index].tr = r;
st[index].lazy = 0;
st[index].mark = 0;
if (l == r) {
st[index].s = 0;
st[index].s2 = 0;
} else {
ll mid = (l + r) >> 1;
build(l, mid, index << 1);
build(mid + 1, r, index << 1 | 1);
push_up(index);
}
}
void add(ll l, ll r, ll d, ll index = 1) {
if (l > st[index].tr or r < st[index].tl)
return;
else if (l <= st[index].tl and st[index].tr <= r) {
st[index].s2 += st[index].s * d * 2 + (st[index].tr - st[index].tl + 1) * d;
st[index].s += (st[index].tr - st[index].tl + 1) * d;
st[index].mark += d;
st[index].lazy = 1;
} else {
if (st[index].lazy)
push_down(index);
add(l, r, d, index << 1);
add(l, r, d, index << 1 | 1);
push_up(index);
}
}
ll query(ll l, ll r, ll index = 1) {
if (l <= st[index].tl and st[index].tr <= r) {
return st[index].s2;
} else {
if (st[index].lazy)
push_down(index);
if (r <= st[index << 1].tr)
return query(l, r, index << 1);
else if (l > st[index << 1].tr)
return query(l, r, index << 1 | 1);
return query(l, r, index << 1) + query(l, r, index << 1 | 1);
}
}
private:
struct SegmentTreeNode {
ll tl;
ll tr;
ll s;//区间和
ll s2;//区间平方和
ll mark;
int lazy;
};
vector<SegmentTreeNode> st;
};
class Solution {
public:
using ll = long long;
ll mod = 1e9 + 7;
int sumCounts(vector<int> &nums) {
int n = nums.size();
unordered_map<int, int> vis;
vector<int> left(n);
for (int i = 0; i < n; i++) {
if (vis.count(nums[i]))
left[i] = vis[nums[i]] + 1;
else
left[i] = 0;
vis[nums[i]] = i;
}
SegmentTree st(n);
ll res = 0;
for (int i = 0; i < n; i++) {
st.add(left[i] + 1, i + 1, 1);
res = (res + st.query(1, i + 1)) % mod;
}
return (res + mod) % mod;
}
};