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];
}
};
天花板
class Solution {
public:
/**
*
* @param n int整型
* @return long长整型
*/
int Find(int n,int i)
{
int x=ceil(1.0*n/i);
int l=i;
int r=n;
while(l<=r)
{
int mid=(l+r)>>1;
if(ceil(1.0*n/mid)<x)
r=mid-1;
else
l=mid+1;
}
return r;
}
long long Sum(int n) {
// write code here
long long ans=0;
for(int i=1,last; i<=n; i=last+1)
{
last=Find(n,i);
ans+=(last-i+1)*int(ceil(1.0*n/i));
}
return ans;
}
};
全部评论
(0) 回帖