String II
class Solution {
public:
int string2(int k, string s) {
int len = s.length(), ret = 0;
for (int key = 1; key <= 26; ++key) {
int ans = 0, tmp = k;
for (int dis = 0; dis < 26; ++dis) {
for (int i = 0; i < len; ++i) {
if (abs(s[i] - ('a' + key - 1)) == dis && tmp >= dis) {
++ans, tmp -= dis;
}
}
}
ret = max(ret, ans);
}
return ret;
}
};
bang!bang!
class Solution {
public:
/**
*
* @param n int整型 乐谱总音符数
* @param m int整型 重音符数
* @param k int整型 重音符之间至少的间隔
* @return long长整型
*/
const static int N = 1010, mod = 1e9 + 7;
long long c[N][N];
long long solve_bangbang(int n, int m, int k) {
// write code here
int d = n - k * (m - 1);
if (d <= 0 || d < m) return 0;
for (int i = 0; i < N; i ++ )
for (int j = 0; j <= i; j ++ )
if (!j) c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
return c[d][m];
}
};
全部评论
(0) 回帖