1688. 比赛中的配对次数
共有n个队伍,一个冠军,需要淘汰n-1个 队伍。
每一场比赛淘汰一个队伍,因此进行了n-1场比赛。
所以共有n-1个配对。
class Solution {
public:
int numberOfMatches(int n) {
return n - 1;
}
};
**1689.十-二进制数的最少数目 **
简单思维
返回所给的字符串中最大的那个数便可以
class Solution {
public:
int minPartitions(string n) {
int x = 0;
for(int i = 0 ;i < n.size(); i ++){
x = max(x,n[i] - '0');
}
return x;
}
};
1690. 石子游戏 VII
前缀和+区间DP+博弈论
博弈论:最坏的情况取最好的
[f[i,j]表示在区间[i,j],先手-后手的分值最大\
s[i,j]表示在区间的和\
决策:先手决定取i还是取j,当取i时,分值为s[i+1,j]-f[i+1][j],当取j时,分值为s[i,j-1]-f[i][j-1]
]
class Solution {
public:
int stoneGameVII(vector<int>& stones) {
int n = stones.size();
vector<int> s(n+1);
for(int i = 1;i <= n;i ++){
s[i] = s[i-1] + stones[i-1];
}
vector<vector<int>> f(n+1,vector<int>(n+1));
for(int len = 2;len <= n;len ++){
for(int i = 1;i + len - 1 <= n;i ++){
int j = i + len - 1;
f[i][j] = max(s[j] - s[i] - f[i+1][j],s[j-1] - s[i-1] - f[i][j-1]);
}
}
return f[1][n];
}
};
1691. 堆叠长方体的最大高度
最大下降子序列
当存在一种方案满足条件时
[a_1>a_2\
b_1>b_2\
c_1>c_2\
对其进行排序之后,仍然满足条件
]
满足条件
[1.合法\
2.c[i]最大且为高\
]
class Solution {
public:
int maxHeight(vector<vector<int>>& w) {
for(auto &x : w) sort(x.begin(),x.end());
sort(w.begin(),w.end(),greater<vector<int>>());
int n = w.size();
vector<int> f(n + 1);
int res = 0;
for(int i = 0; i < n;i ++){
f[i] = w[i][2];
for(int j = 0;j < i;j ++){
if(w[j][0] >= w[i][0] && w[j][1] >= w[i][1] && w[j][2] >= w[i][2])
f[i] = max(f[i],f[j] + w[i][2]);
}
res = max(res,f[i]);
}
return res;
}
};