1.这道题的考察次数还可以,解法比较简单,找最低位1,然后将其置为0,dp就完了。
1.这道题的解法也很简单,就直接dp就完事儿了。
2.切记,先让details[0] = 0,咱们就直接让数字和index下标匹配,就好了。且,details[2] = 2而不是1啊!!!因为别人拿到2以后完全可以不用分解啊,同样的道理,details[3] = 3
1.考察次数非常多,不过这道题思路非常简单,就map统计,然后再反过来放到优先队列就完事。
1.送分题,pass
2.其实这道题可以维护行和列的和,让玩家分别表示成-1和1,那么胜出的条件就很简单了。
1.思路也是非常清晰的,就dfs完事儿了,记得有进有出,维护这个公共的vector
1.结构应该是比较清晰的,首先要维护关注关系,A关注了B但不代表B关注了A。本来我计划用二维数组,但是这里就算了,直接map吧,first是userID,second是一个vector,存放关注的人
2.再维护一个map,first还是userID,second是一个list,保存自己发送的东西,每次都插入到头部,同时我建议加入一个fake_head,里面保存这个list真实的数量。当超过10的时候请抛弃掉后面的东西。
1.这道题的思路还是比较清晰的,遍历所有坐标,找到最大Y和最小Y,那么这个中间值就只能是一半。
2.将所有的元素放入set中,然后开始遍历每个元素,做查找,就完了。
1.非常棒的一道题目,思路还是很棒的,dp解决。
2.当n超过范围时,多亏了阶乘函数的帮助,乘了个0,就没有反向增长。
1.这道题其实很简单,就是个抛物线,或者是个直线,反正最多双向趋势,那么直接双指针就完事儿。
2.建议认真复习这道题。
1.也是没能想到正确的解题思路,看了题解才了解。
2.因为限制了每次操作只能是倒空X、倒空Y、倒满X、倒满Y、把X倒入Y中直到满或空、把Y倒入X中直到满或空,那就bfs不就完事儿了么!!!
3.这道题目是真的棒!
1.这道题也很棒啊,dfs确实是清清楚楚,具体逻辑是:先处理自己是空、再处理自己是叶子节点(一般只有上来才是叶子)、最后处理孩子。其实这次的dfs主要是针对孩子展开的。如果这个节点的孩子是孤儿,就将其删除并加入vector,否则就dfs进去。
class Solution {
private:
vector<vector<int>>ans;
private:
void dfs(TreeNode* root, vector<int>& details) {
if(root == nullptr)
return;
if(root->left == nullptr && root->right == nullptr) {
details.push_back(root->val);
delete root;
root = nullptr;
} else {
if(root->left != nullptr) {
if(root->left->left == nullptr && root->left->right == nullptr) {
details.push_back(root->left->val);
delete root->left;
root->left = nullptr;
} else {
dfs(root->left, details);
}
}
if(root->right != nullptr) {
if(root->right->left == nullptr && root->right->right == nullptr) {
details.push_back(root->right->val);
delete root->right;
root->right = nullptr;
} else {
dfs(root->right, details);
}
}
}
}
public:
vector<vector<int>> findLeaves(TreeNode* root) {
while(root != nullptr) {
vector<int> details;
dfs(root, details);
ans.push_back(details);
}
return ans;
}
};
1.这道题是真的妙啊!dp就完事儿了。加上记录路径,太妙了。
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> ans;
if(nums.size() != 0) {
vector<int> details(nums.size(), 0);
details[0] = 1;
vector<int> path(nums.size(), 0);
path[0] = -1;
sort(nums.begin(), nums.end());
for(int i = 1; i < nums.size(); ++ i) {
int tempMax = 1, tempPath = -1;
for(int j = 0; j < i; ++ j) {
if(nums[i]%nums[j] == 0) {
if(tempMax < details[j] + 1) {
tempMax = details[j] + 1;
tempPath = j;
}
}
}
details[i] = tempMax;
path[i] = tempPath;
}
int MaxLength = 0, curIndex = -1;
for(int i = 0; i < details.size(); ++ i) {
if(details[i] > MaxLength) {
MaxLength = details[i];
curIndex = i;
}
}
while(curIndex != -1) {
ans.push_back(nums[curIndex]);
curIndex = path[curIndex];
}
reverse(ans.begin(), ans.end());
}
return ans;
}
};
1.题目还是蛮复杂的,给的是无向关系,自然先用邻接表组织起来。然后开始用bfs,把关系捋顺,既然选择了0作为起点,那么就不能保存1->0的关系。
2.下一步就是dfs,记得返回每个char对应的数量,这样的话就方便dfs了。
class Solution {
private:
unordered_map<char, int> dfs(int index, vector<vector<int>>&relations, vector<int>& ans, string&labels){
unordered_map<char, int> res;
if(relations[index].size() == 0) {
ans[index] = 1;
res[labels[index]]++;
return res;
} else {
for(int i = 0; i < relations[index].size(); ++ i) {
unordered_map<char, int> temp = dfs(relations[index][i], relations, ans, labels);
for(auto iter = temp.begin(); iter != temp.end(); ++iter) {
res[iter->first] += iter->second;
}
}
res[labels[index]] ++;
ans[index] = res[labels[index]];
return res;
}
}
public:
vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
vector<int> ans(n, 0);
vector<vector<int>> relations(n, vector<int>());
for(int i = 0; i < edges.size(); ++ i) {
relations[edges[i][0]].push_back(edges[i][1]);
relations[edges[i][1]].push_back(edges[i][0]);
}
queue<int> details;
details.push(0);
while(details.empty() == false) {
int target = details.front();
details.pop();
for(int i = 0; i < relations[target].size(); ++ i) {
auto iter = find(relations[relations[target][i]].begin(), relations[relations[target][i]].end(), target);
relations[relations[target][i]].erase(iter);
details.push(relations[target][i]);
}
}
unordered_map<char, int> res = dfs(0, relations, ans, labels);
return ans;
}
};