leetcode weekly contest 43
leetcode weekly contest 43
leetcode649. Dota2 Senate
leetcode649.Dota2 Senate
思路:
模拟规则round by round。
class Solution {
public:
string predictPartyVictory(string senate) {
int len = senate.size();
int r = 0;
int d = 0;
int i = 0;
while(1)
{
if(senate[i] == 'R')
{
int j = 1;
while(senate[(i + j) % len] != 'D' && j < len) j++;
if(senate[(i + j) % len] == 'R') return "Radiant";
else senate[(i + j) % len] = '#';
}
else if(senate[i] == 'D')
{
int j = 1;
while(senate[(i + j) % len] != 'R' && j < len) j++;
if(senate[(i + j) % len] == 'D') return "Dire";
else senate[(i + j) % len] = '#';
}
i = (i + 1) % len;
}
}
};
leetcode650. 2 Keys Keyboard
leetcode650. 2 Keys Keyboard
思路:
找到最大的约数,dp。
注意copy算一步。
class Solution {
public:
int find(int x)
{
for(int i = 2; i <= sqrt(x); i++)
{
if(x % i == 0) return i;
}
return x;
}
int minSteps(int n) {
vector<int> res(n + 1, 0);
int temp;
for(int i = 2; i <= n; i++)
{
temp = find(i);
res[i] = res[i / temp] + temp;
}
return res[n];
}
};
leetcode651. 4 Keys Keyboard
leetcode651. 4 Keys Keyboard
思路:
dp.
res[n] = max{res[n - 1] + 1, res[n - 5] * 4, res[n - 4] * 3, res[n - 3] * 2};
时间O(n),空间O(1)
class Solution {
public:
int maxA(int N) {
//vector<int> res(N + 1);
int four,three,two,one,five,res;
five = four = three = two = one = res = 0;
for(int i = 1; i <= N; i++)
{
res = max(one + 1,max(four * 3, max(three * 2,five * 4)));
five = four;
four = three;
three = two;
two = one;
one = res;
}
return res;
}
};
leetcode652. Find Duplicate Subtrees
leetcode652. Find Duplicate Subtrees
思路:
这个思路我感觉特别地棒。 既然是找duplicates,可以直接用map。吧树给serialize了变成一个string,让map自己比较。然后找到全部的duplicates。
时间O(n)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
unordered_map<string,vector<TreeNode*> > map;
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
vector<TreeNode*> res;
serialize(root);
unordered_map<string,vector<TreeNode*>>::iterator it = map.begin();
while(it != map.end())
{
if(it->second.size() > 1 && it->first != "")
{
res.push_back(it->second[0]);
}
it++;
}
return res;
}
private:
string serialize(TreeNode* root)
{
if(!root) return "";
string s = '(' + serialize(root->left) + to_string(root->val) + serialize(root->right) + ')';
map[s].push_back(root);
return s;
}
};
leetcode weekly contest 43的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- LeetCode Weekly Contest 117
已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...
随机推荐
- 看到了一篇博文,关于网卡的sniff模式,感觉相当好
@font-face { font-family: "Times New Roman"; }@font-face { font-family: "宋体"; }@ ...
- 设置网卡IP,还每次都挨个地址输入吗?批处理一下【转】
1.设置网卡ip,子网掩码和默认网关,注意修改网卡名称,跟本地连接汇总的网卡名称保持一直 netsh interface ip set address "以太网" static 1 ...
- 123.Best Time to Buy and Sell Stock III---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目大意:与122题类似,只是这 ...
- 016 sleep,wait,yield,join区别
1.线程通常有五种状态,创建,就绪,运行.阻塞和死亡状态.2.阻塞的情况又分为三种:(1).等待阻塞:运行的线程执行wait()方法,该线程会释放占用的所有资源,JVM会把该线程放入“等待池”中.进入 ...
- pip安装模块时:error: command 'gcc' failed with exit status 1
用安装python模块出现error: command 'gcc' failed with exit status 1 问题: gcc编译缺少模块 解决方法: yum install gcc libf ...
- Linux 基础——常用的Linux网络命令
一.学Linux网络命令有什么好处 网络的出现,我们的生活更方便了,处理事情的效率也越来越高,也可以看到全世界文化的差异.同时我们接受新事物的信息越来越来强,新事物的信息也越来越来多.网络对于我们尔等 ...
- 在LoadRunner中从数组类型的参数随机取值的方法
在LoadRunner中从数组类型的参数随机取值的方法 使用web_reg_save_param做关联后,有时候会有多个匹配值. 为了模仿用户行为随机取一个值为后续transcation所用,可以使用 ...
- Wannafly挑战赛18 C - 异或和
思路:我刚开始是想旋转四次坐标,每次用bit计算每个点左上角的点到这个点的距离,TLE了.... 这种算曼哈顿距离的可以将x 轴和 y 轴独立开来,分别计算. #include<bits/std ...
- (转)Where与Having的总结
Where 是一个约束声明,使用Where来约束来之数据库的数据,Where是在结果返回之前起作用的,且Where中不能使用聚合函数. Having 是一个过滤声明,是在查询返回结果集以后对查询结果进 ...
- [Codeforces166B]Polygons 凸包
大致题意: 给你一个凸多边形A,和一个任意多边形B,判断B是否在A的内部 先对A的点集建凸包,然后枚举B中的点,二分判断是否在A的内部. 二分时可用叉积判断,详细见代码 #include<cst ...