A 移除字符串中的尾随零
模拟
class Solution {
public:
string removeTrailingZeros(string num) {
while(num.back()=='0')
num.pop_back();
return num;
}
};
B 对角线上不同值的数量差
还是模拟…
class Solution {
public:
vector<vector<int>> differenceOfDistinctValues(vector<vector<int>> &g) {
int m = g.size(), n = g[0].size();
vector<vector<int>> res(m, vector<int>(n));
vector<int> visleft(51), visright(51);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
fill(visleft.begin(), visleft.end(), 0);
fill(visright.begin(), visright.end(), 0);
for (int r = 0; r < m; r++) {
int c = r - i + j;
if (c >= 0 && c < j)
visleft[g[r][c]] = 1;
else if (c > j && c < n)
visright[g[r][c]] = 1;
}
res[i][j] = abs(count(visleft.begin(), visleft.end(), 1) - count(visright.begin(), visright.end(), 1));
}
return res;
}
};
C 使所有字符相等的最小成本
动态规划: 定义
s
u
f
i
,
j
suf_{i,j}
sufi,j为将后缀s[i,n-1]通过第二种操作转化全为j的最小代价,有状态转移方程:
s
u
f
i
,
0
=
{
s
u
f
i
+
1
,
1
+
n
−
i
if
s
i
=
′
1
′
s
u
f
i
+
1
,
0
if
s
i
=
′
0
′
suf_{i,0}=\left\{\begin{matrix} suf_{i+1,1} + n-i & \text{ if } s_i='1' \\ suf_{i+1,0} & \text{ if } s_i='0' \end{matrix}\right.
sufi,0={sufi+1,1+n−isufi+1,0 if si=′1′ if si=′0′
s
u
f
i
,
1
=
{
s
u
f
i
+
1
,
0
+
n
−
i
if
s
i
=
′
0
′
s
u
f
i
+
1
,
1
if
s
i
=
′
1
′
suf_{i,1}=\left\{\begin{matrix} suf_{i+1,0} + n-i & \text{ if } s_i='0' \\ suf_{i+1,1} & \text{ if } s_i='1' \end{matrix}\right.
sufi,1={sufi+1,0+n−isufi+1,1 if si=′0′ if si=′1′
类似地,定义
p
r
e
i
,
j
pre_{i,j}
prei,j为将前缀s[0,i]通过第一种操作转化全为j的最小代价,有状态转移方程:
p
r
e
i
,
0
=
{
p
r
e
i
−
1
,
1
+
i
+
1
if
s
i
=
′
1
′
p
r
e
i
−
1
,
0
if
s
i
=
′
0
′
pre_{i,0}=\left\{\begin{matrix} pre_{i-1,1} + i+1 & \text{ if } s_i='1' \\ pre_{i-1,0} & \text{ if } s_i='0' \end{matrix}\right.
prei,0={prei−1,1+i+1prei−1,0 if si=′1′ if si=′0′
p
r
e
i
,
1
=
{
p
r
e
i
−
1
,
0
+
i
+
1
if
s
i
=
′
0
′
p
r
e
i
+
1
,
1
if
s
i
=
′
1
′
pre_{i,1}=\left\{\begin{matrix} pre_{i-1,0} + i+1 & \text{ if } s_i='0' \\ pre_{i+1,1} & \text{ if } s_i='1' \end{matrix}\right.
prei,1={prei−1,0+i+1prei+1,1 if si=′0′ if si=′1′
枚举前两种操作的划分位置即可
class Solution {
public:
long long minimumCost(string s) {
int n = s.size();
long long pre[n][2], suf[n][2];
suf[n - 1][0] = s[n - 1] == '0' ? 0 : 1;
suf[n - 1][1] = s[n - 1] == '1' ? 0 : 1;
for (int i = n - 2; i >= 0; i--) {
suf[i][0] = s[i] == '1' ? suf[i + 1][1] + n - i : suf[i + 1][0];
suf[i][1] = s[i] == '0' ? suf[i + 1][0] + n - i : suf[i + 1][1];
}
pre[0][0] = s[0] == '0' ? 0 : 1;
pre[0][1] = s[0] == '1' ? 0 : 1;
for (int i = 1; i < n; i++) {
pre[i][0] = s[i] == '1' ? pre[i - 1][1] + i + 1 : pre[i - 1][0];
pre[i][1] = s[i] == '0' ? pre[i - 1][0] + i + 1 : pre[i - 1][1];
}
long long res = min({pre[n - 1][0], pre[n - 1][1], suf[0][0], suf[0][1]});
for (int i = 0; i < n - 1; i++) {//s[0,i]上用第一种操作,s[i+1,n-1]上用第二种操作
res = min(res, pre[i][0] + suf[i + 1][0]);
res = min(res, pre[i][1] + suf[i + 1][1]);
}
return res;
}
};
D 矩阵中严格递增的单元格数
枚举: 按元素大小升序枚举矩阵中的元素,同时维护当前结尾在各行各列上最长严格递增的单元格序列的长度,注意值相等的元素要同时更新各行列上的最长长度
class Solution {
public:
int maxIncreasingCells(vector<vector<int>> &g) {
int m = g.size(), n = g[0].size();
vector<int> rmx(m), cmx(n);//rmx[i]: 矩阵第i行存在这样的元素g[i][_]:存在长度为rmx[i]以g[i][_]为结尾的升序单元格序列, cmx[i]: 矩阵第i列存在这样的元素g[_][i]:存在长度为cmx[i]以g[_][i]为结尾的升序单元格序列
vector<tuple<int, int, int>> li;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
li.emplace_back(g[i][j], i, j);
sort(li.begin(), li.end());
int N = m * n;
for (int i = 0; i < N;) {
int j = i;
while (j + 1 < N && get<0>(li[j + 1]) == get<0>(li[i]))
j++;//同时处理值相等的方格
vector<int> temp(j - i + 1);
for (int k = i; k <= j; k++) {
auto [vk, rk, ck] = li[k];
temp[k - i] = 1 + max(rmx[rk], cmx[ck]);//当前方格与前一个方格要么同行要么同列
}
for (int k = i; k <= j; k++) {
auto [vk, rk, ck] = li[k];
rmx[rk] = max(rmx[rk], temp[k - i]);
cmx[ck] = max(cmx[ck], temp[k - i]);
}
i = j + 1;
}
return *max_element(rmx.begin(), rmx.end());
}