牛客编程巅峰赛S2第7场 - 钻石&王者T1
简单的二分答案
#题解#
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param x string字符串
* @return int整型
*/
bool check(const string &s, int x) {
char c = 'a';
int t = 0;
for (char i : s)
if (c <= 'c') {
if (i == c) {
t++;
if (t == x)
c++, t = 0;
}
}
return c == 'd';
}
int Maximumlength(string x) {
int l = 1, r = x.size(), ans = 0;
while (l <= r) {
int mid = l + r >> 1;
if (check(x, mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
return ans * 3;
}
};
#题解#
正浩创新EcoFlow公司福利 707人发布