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 ...
随机推荐
- React 16 源码瞎几把解读 【二】 react组件的解析过程
一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...
- 在rhel7上搭建centos7的yum源
1. 再查看现在主机上的yum源,并将它们删除 [root@localhost ~]# rpm -qa|grep yum | xargs rpm -e --nodeps # --nodeps 不管有没 ...
- Python——文件打开模式辨析
版权声明:本文系原创,转载请注明出处及链接. Python中,open()函数打开文件时打开模式如r.r+ .w+.w.a.a+有何不同 r 只能读 r+ 可读可写,不会创建不存在的文件.如果直接写文 ...
- python多线程下载文件
从文件中读取图片url和名称,将url中的文件下载下来.文件中每一行包含一个url和文件名,用制表符隔开. 1.使用requests请求url并下载文件 def download(img_url, i ...
- Python 2 到 Python 3 的变化
1: commands 被 subprocess 所取代:举例 Python2中使用getoutput: >>> import commands >>> print ...
- CoreOS中随着系统启动Docker Container
Start containers automatically https://docs.docker.com/engine/admin/host_integration/ https://www.fr ...
- <node.js爬虫>制作教程
前言:最近想学习node.js,突然在网上看到基于node的爬虫制作教程,所以简单学习了一下,把这篇文章分享给同样初学node.js的朋友. 目标:爬取 http://tweixin.yueyishu ...
- Wannafly挑战赛18 C - 异或和
思路:我刚开始是想旋转四次坐标,每次用bit计算每个点左上角的点到这个点的距离,TLE了.... 这种算曼哈顿距离的可以将x 轴和 y 轴独立开来,分别计算. #include<bits/std ...
- ffmpeg 编译graph2dot
cd ffmpeg ./configure make make tools/graph2dot
- 第12课:Spark Streaming源码解读之Executor容错安全性
一.Spark Streaming 数据安全性的考虑: Spark Streaming不断的接收数据,并且不断的产生Job,不断的提交Job给集群运行.所以这就涉及到一个非常重要的问题数据安全性. S ...