目录
- 1. 递枕头
- 2. 二叉树中的第 K 大层和
- 3. 分割数组使乘积互质
- 4. 获得分数的方法数
1. 递枕头
class Solution {
public:
int passThePillow(int n, int time) {
int mor = time%(2*(n-1));
if(mor >= n-1){
return n-(mor-(n-1));
}else{
return mor+1;
}
}
};
总结:
按题意模拟即可
2. 二叉树中的第 K 大层和
class Solution {
public:
long long cnt[100010];
int maxpos = 0;
void dfs(TreeNode* root, int pos)
{
if(root == NULL){
return;
}
maxpos = max(maxpos, pos);
cnt[pos] += root->val;
dfs(root->left, pos+1);
dfs(root->right, pos+1);
}
long long kthLargestLevelSum(TreeNode* root, int k) {
dfs(root, 1);
if(k > maxpos){
return -1;
}
sort(cnt+1, cnt+maxpos+1);
return cnt[maxpos-k+1];
}
};
总结:
dfs跑一遍树, 用数组记录每层的层和
3. 分割数组使乘积互质
class Solution {
public:
int isPrime[1010];
int Prime[1000010], cnt;
void init()
{
isPrime[1] = 1;
for(int i=2;i& nums) {
init();
int len = nums.size();
for(int i=0;i 1){
++cnt2[temp];
Prime[++cnt] = temp;
}
}
for(int i=0;i 1){
--cnt2[temp];
++cnt1[temp];
}
if(check()){
return i;
}
}
return -1;
}
};
总结:
题目意思转换一下, 变为分割的前半部分和后半部分分解为质因数后没有相同的质因数, 数据范围小于1e6, 质数筛筛到1e3即可, 因为每个数最多只会有一个大于1000的质因数, 大于1000的质因数在分解质因数是放入Prime数组.接下来模拟即可.
感觉写的不好
4. 获得分数的方法数
class Solution {
public:
const int mod = 1e9+7;
int dp[60][1010];
int waysToReachTarget(int target, vector>& types) {
int len = types.size();
dp[0][0] = 1;
for(int i=1;i=0;--l){
dp[i][l] = dp[i-1][l];
}
int count = types[i-1][0], marks = types[i-1][1];
for(int j=count;j>=1;--j){
for(int l=target;l>=j*marks;--l){
dp[i][l] += dp[i-1][l-j*marks];
dp[i][l] %= mod;
}
}
}
return dp[len][target];
}
};
总结:
一个比较裸的多重背包
感觉写的不好
机房租用,北京机房托管,大带宽租用,IDC机房服务器主机租用托管-价格及服务咨询 www.e1idc.net