Contest 51 (2018年11月22日,周四早上)(题号681-684)

链接:https://leetcode.com/contest/leetcode-weekly-contest-51

比赛结果记录:3/4,ranking:270/2879。第三题有点类似于图的最小生成树,第四题似乎是很火的种花题(第四题我好像几个月之前做过,有印象,当时也是自己做的)。

【682】Baseball Game(第一题 3分)

You're now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round's operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example :
Input: ["","","C","D","+"]
Output:
Explanation:
Round : You could get points. The sum is: .
Round : You could get points. The sum is: .
Operation : The round 's data was invalid. The sum is: 5.
Round : You could get points (the round 's data has been removed). The sum is: 15.
Round : You could get + = points. The sum is: . Example :
Input: ["","-2","","C","D","","+","+"]
Output:
Explanation:
Round : You could get points. The sum is: .
Round : You could get - points. The sum is: .
Round : You could get points. The sum is: .
Operation : The round 's data is invalid. The sum is: 3.
Round : You could get - points (the round 's data has been removed). The sum is: -1.
Round : You could get points. The sum is: .
Round : You could get - + = points. The sum is .
Round : You could get + = points. The sum is . Note:
The size of the input list will be between and .
Every integer represented in the list will be between - and .

题解:模拟题,用了一个deque做的模拟,应该可以有不用数据结构的方法?2 pass

 class Solution {
public:
int calPoints(vector<string>& ops) {
deque<int> dq;
const int n = ops.size();
for (auto s : ops) {
if (s == "C") {
if (!dq.empty()) {
dq.pop_back();
}
} else if (s == "D") {
if (!dq.empty()) {
dq.push_back(dq.back() * );
}
} else if (s == "+") {
int numA = ;
if (!dq.empty()) {
numA = dq.back();
dq.pop_back();
if (!dq.empty()) {
int temp = dq.back() + numA;
dq.push_back(numA);
dq.push_back(temp);
} else {
dq.push_back(numA);
}
}
} else {
dq.push_back(stoi(s));
}
}
int ret = ;
while (!dq.empty()){
ret += dq.front();
dq.pop_front();
}
return ret;
}
};

【681】Next Closest Time(第二题 6分)

给了一个时间表示,格式是 "HH:MM",用现在时间表示的这些数字形成下一个距离现在最近的时间字符串,并且返回.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example :
Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits , , , , is :, which occurs minutes later. It is not :, because this occurs hours and minutes later. Example :
Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits , , , , is :. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

题解:感觉和产生下一个回文串比较类似,先把这四个数字记下来,然后从后往前找第一个在合理的范围内能变得更大的数字。因为是时间,所以时针最多到23,分针最多到59,找到第一个能变得更大的数字就把它变得更大,然后把它后面的数字都变成最小。如果找不到这样的一数字,比如eg2的 23:59,就把所有的数字变成最小的数字,算作新的一天。

 class Solution {
public:
string nextClosestTime(string time) {
set<int> st;
int minn = ;
for (int i = ; i < ; ++i) {
if (isdigit(time[i])) {
int t = time[i] - '';
minn = min(t, minn);
st.insert(t);
}
}
if (st.size() == ) {return time;} //invalid
string ret = time;
for (int i = ; i >= ; --i) {
char c = ret[i];
int t = c - '';
auto iter = upper_bound(st.begin(), st.end(), t);
if (iter == st.end()) {continue;}
if (i == && *iter > ) {
continue;
}
if (i == && *iter > && ret[] == '') {
continue;
}
if (i == && *iter > ) {
continue;
}
ret[i] = (*iter) + '';
for (int k = i + ; k < ; ++k) {
if (isdigit(ret[k])) {
ret[k] = minn + '';
}
}
break;
}
if (ret == time) {
for (auto& c : ret) {
if (isdigit(c)) {
c = minn + '';
}
}
}
return ret; }
};

【684】Redundant Connection(第三题 8分)

在本题中,树是一个无环的无向图。输入一个N个结点(编号是1~N)的图,有一条边是多余的,把这条边找出来。

Example :
Input: [[,], [,], [,]]
Output: [,]
Explanation: The given undirected graph will be like this: / \
- Example :
Input: [[,], [,], [,], [,], [,]]
Output: [,]
Explanation: The given undirected graph will be like this:
- -
| |
- Note:
The size of the input 2D-array will be between and .
Every integer represented in the 2D-array will be between and N, where N is the size of the input array.

题解:我是用并查集解的。对于每一条边的两个结点,如果他们的爸爸不是同一个爸爸,那么就 unoin 这两个结点,如果他们两个的爸爸是同一个爸爸,就说明这条边多余了,直接返回这条边就行了。

 class Solution {
public:
int findfather(int x) {
return x == father[x] ? x : findfather(father[x]);
}
void unionNode(int x, int y) {
x = findfather(x);
y = findfather(y);
if (x == y) { return; }
father[y] = x;
} vector<int> findRedundantConnection(vector<vector<int>>& edges) {
n = edges.size();
father.resize(n+); //redundant 0
for (int i = ; i < n+; ++i) {
father[i] = i;
}
vector<int> ret;
for (auto e : edges) {
int u = min(e[], e[]), v = max(e[], e[]);
if (findfather(v) == findfather(u)) {
ret = e;
break;
} else {
unionNode(u, v);
}
}
return ret;
}
int n = ;
vector<int> father; };

【683】K Empty Slots(第四题 9分)(经典的种花题)

给了 N 个花槽,有 N 朵花每天开一朵,用 flower[i] = x 表示在第 i 天在位置 x 开出了一朵花,花开了之后就会一直开着,不会凋谢。问是否存在这么一天,某个位置的一朵花开了,然后另外一朵花也在开花的状态,这两朵花中间有恰好有 k 个花槽,这 k 个花槽都没有开花。

题解:这题其实之前做过,第一次做就会做,有点倒排索引的感觉。我们用另外一个数组 bloom[pos] = day,表示第pos个位置的花在第 day 天开。然后我们从第二天开始枚举,找到第二天开花的位置,然后用 k 计算出另外一朵花的位置,看是否已经开花,如果已经开花了,就看一下中间的 k 个花槽是不是都没开花,如果都满足条件,直接返回这一天。

我今天第一次写的时候犯了一个误区,就是我错误的认为如果今天开花,那么它对应的另外一朵花一定是昨天开的,事实上它对应的花可以在今天前面的任何一天开花。所以会错。

 //这题一开始有个思维误区,就是我懵逼的认为如果满足条件的那天开的那朵花,与这朵花相对应的另外一朵一定开在满足条件的前一天orz。
class Solution {
public:
int kEmptySlots(vector<int>& flowers, int k) {
const int n = flowers.size();
vector<int> bloom(n + , );
for (int i = ; i < flowers.size(); ++i) {
int day = i+, pos = flowers[i]; //day (1, n)
bloom[pos] = day;
}
//find pos flowers[day-1]
for (int day = ; day < n; ++day) {
int posToday = flowers[day-];
for (int m = ; m < ; ++m) {
int posPre = m == ? (posToday + k + ) : (posToday - k - );
if (posPre > && posPre <= n) {
int preDay = bloom[posPre];
if (preDay > day) {continue;}
bool valid = true;
for (int i = min(posPre, posToday) + ; i < max(posPre, posToday); ++i) {
if (bloom[i] < day) {
valid = false;
break;
}
}
if (valid) {return day;}
}
}
}
return -;
}
};

Contest 52 (2018年11月24日,周六下午)(题号686-689)

比赛情况记录:2/4,rank:485/2615. 第三题概率题不会做,第四题感觉方法对了,但是代码写出来踩内存了RE,调试了一晚上也没搞明白到底哪里问题。mdzz。

链接:https://leetcode.com/contest/leetcode-weekly-contest-52

【686】Repeated String Match(第一题 3分)

第一题给了两个字符串,A 和 B, 问 A 倍增几倍之后能不能把 B 变成它的子串。返回 A 倍增的最小次数,如果无论怎么倍增 B 都不是子串的话,返回 -1。

题解:本题一开始还有点卡住了orz,尴尬。先求 A B 的长度,如果 A 的长度直接比 B 大的话,最多 2 个 A可以搞成 B。同理,先求 Asize / Bsize,先把 A 的长度倍增成比B大一点点或者相等,如果这时候 B 还不是 A 的子串,就把再加一个 A,如果加了还不是就是不是了。

 class Solution {
public:
int repeatedStringMatch(string A, string B) {
const int Asize = A.size(), Bsize = B.size();
int t = Bsize / Asize;
if (Asize * t < Bsize) {++t;}
string newA = "";
for (int i = ; i < t; ++i) {
newA += A;
}
if (newA.find(B) == string::npos) {
newA += A;
if (newA.find(B) == string::npos) {
return -;
}
return t+;
}
return t;
}
};

【687】Longest Univalue Path(第二题 5分)

【688】Knight Probability in Chessboard(第三题 6分)

【689】Maximum Sum of 3 Non-Overlapping Subarrays(第四题 8分)

Contest 53 ()(题号)

Contest 54 ()(题号)

Contest 55 ()(题号)

Contest 56 ()(题号)

Contest 57 ()(题号)

Contest 58 ()(题号)

Contest 59 ()(题号)

Contest 60 (2018年12月26日,周三早上)(题号)

链接:https://leetcode.com/contest/weekly-contest-60

比赛结果记录:3/4,没开 virtual,所以我也没有记录具体的时间。orz。第四题研究了一会儿,需要递归做。然而自己放弃了没写出来。

【733】Flood Fill(第一题 3分)

给了一个二维的matrix,和一个点坐标,和新颜色。把这个点周围四联通区域和该点相同颜色的点,都染成新的颜色。

题解:无

 class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
n = image.size(), m = image[].size();
oric = image[sr][sc];
if (oric == newColor) {return image;}
floodfill (image, sr, sc, newColor);
return image;
}
int n, m;
int oric;
void floodfill(vector<vector<int>>& image, int cur_x, int cur_y, const int color) {
image[cur_x][cur_y] = color;
for (int i = ; i < ; ++i) {
int newx = cur_x + dirx[i], newy = cur_y + diry[i];
if (newx < || newx >= n || newy < || newy >= m || image[newx][newy] != oric) {
continue;
}
floodfill(image, newx, newy, color);
}
}
int dirx[] = {-, , , };
int diry[] = {, -, , };
};

【734】Sentence Similarity(第二题 3分)

给了两个单词数组,words1,words2,和一个相似单词的词典,判断这两个单词数组是不是相似。相似的条件为(1)两个数组元素个数一致;(2)words1[i], words2[i] 在相似词典中有映射关系,或者words1[i] 和 words2[i] 这两个单词相同。

题解:无

 class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
const int n1 = words1.size(), n2 = words2.size();
if (n1 != n2) {return false;}
set<pair<string, string>> st(pairs.begin(), pairs.end());
bool flag = true;
for (int i = ; i < n1; ++i) {
string s1 = words1[i], s2 = words2[i];
if (s1 == s2) {continue;}
pair<string, string> p1 = make_pair(s1, s2), p2 = make_pair(s2, s1);
if (st.find(p1) == st.end() && st.find(p2) == st.end()) {
flag = false;
break;
}
}
return flag;
}
};

【735】Asteroid Collision(第三题 5分)

给了一个数组 asteroids 代表一排小行星,元素绝对值代表行星大小,正数代表行星向右,负数代表向左。(如果前面行星向右,后面行星向左,就一定会相撞。但是如果同向或者相背,就肯定不会相撞)如果两颗行星相撞,小的会爆炸,如果大小相同就都爆炸。问最后剩下的行星,返回这个数组。

题解:用一个 vector 或者 deque 存储已经遍历过的行星,如果当前元素是负数,就要一直和前面的正数行星相撞,直到前面没有正数行星为止。有点类似业务逻辑题了。时间复杂度是 O(N)。本题用vector更合适。

 class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
const int n = asteroids.size();
deque<int> dq;
for (auto ast : asteroids) {
if (dq.empty()) {
dq.push_back(ast);
} else {
bool needPush = true;
while (!dq.empty() && dq.back() > && ast < ) {
if (abs(dq.back()) == abs(ast)) {
dq.pop_back();
needPush = false;
break;
} else if (abs(dq.back() < abs(ast))) {
dq.pop_back();
} else {
needPush = false;
break;
}
}
if (needPush) {
dq.push_back(ast);
}
}
}
vector<int> ret(dq.size());
int i = ;
while (!dq.empty()) {
ret[i++] = dq.front();
dq.pop_front();
}
return ret;
}
};

【】Parse Lisp Expression(第四题 9分)

【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)的更多相关文章

  1. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  2. 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)

    注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...

  3. 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)

    Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...

  4. 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

    Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...

  5. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  6. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  7. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  8. 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)

    [LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...

  9. 【Leetcode周赛】比赛目录索引

    contest 1 ~ contest 10: contest 11 ~ contest 20: contest 21 ~ contest 30 : https://www.cnblogs.com/z ...

随机推荐

  1. CollectionUtils工具类中常用方法

    @SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...

  2. Bugku 杂项 签到题

    签到题 加微信公众号会发现

  3. Android中对Apk加固(加壳)续篇之---对Native层(so文件)进行加固

    有人说Android程序用Java代码写的,再怎么弄都是不安全的,很容易破解的,现在晚上关于应用加固的技术也很多了,当然这些也可以用于商业发展的,梆梆加密和爱加密就是很好的例子,当然这两家加固的Apk ...

  4. [Ynoi2012]D1T3

    https://www.luogu.org/problemnew/show/P5311 题解 先把点分树建出来. 对于吗,每一个询问\((l,r,x)\),我们对于x要找到它在点分树上最靠上的父亲节点 ...

  5. HTML与CSS中的文本个人分享

    文本 标题元素 注意: 在一个HTML页面中最好只使用一个<h1>标题 因为浏览器只会抓取一个多了没用 示例代码: <body> <!-- 标题元素 - <h1&g ...

  6. HTML 解析类库HtmlAgilityPack

    1. HtmlAgilityPack简介 网站中首先遇到的问题是爬虫和解析HTML的问题,一般情况在获取页面少量信息的情况下,我们可以使用正则来精确匹配目标.不过本身正则表达式就比较复杂,同时正则表达 ...

  7. 安卓中如何调用jni

    ##指针和数组之间的关系 * 数组名就是数组元素的首地址 * 数组是一块连续的内存空间,每个元素之间的距离跟数据的类型有关 * 数组名字取地址,得到的还是数组的首地址 * arr[i]  ==  *( ...

  8. ELK+Filebeat (2)

    ELK+Filebeat收集多台机器不同日志 采坑:在使用了6.0版本的ELK以后,使用如上配置,if [type]匹配不到在filebeat里面使用document_type定义的字符串.在多次调试 ...

  9. Linux libOpenThreads库链接冲突错误

    最近在linux 上安装了3.7.0版本的OpenSceneGraph,而在安装之前没有完全卸载之前安装的3.6.3版本,导致在编译程序链接时出现库引用冲突,在便以后出现以下警告信息: /usr/bi ...

  10. 用vultr搭建ss服务器的脚本

    原文在此