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. 一些vue 响应式系统的底层的细节

    当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Object.defineProperty 把这些属性全部转为 getter/ ...

  2. LeetCode--046--全排列(java)

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...

  3. Android9.0特性

    这篇文章,是Android官方文档的中文版本. 注意事项(AndroidP 特性): (1),android.os.Build.VERSION.RELEASE ,需要当做字符串类型处理. (2),依赖 ...

  4. python设置文字输出颜色

    #!/usr/bin/env python # -*- coding:utf-8 -*- """ @Time: 2018/5/5 20:43 @Author: Jun H ...

  5. PHPcms编辑器如何粘贴带格式的word文档

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...

  6. asp.net+扫描仪+图片上传

    问题: IE浏览器下使用Activex插件调用客户端扫描仪扫描文件并山传,可以将纸质档案(如合同.文件.资料等)扫描并将扫描图像保存到服务器,可以用于合同管理.档案管理等. 通过插件方式调用扫描仪扫描 ...

  7. logback-spring.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false ...

  8. MySQL定义数据库对象之指定definer

    mysql创建view.trigger.function.procedure.event时都会定义一个Definer: SQL SECURITY 有两个选项,一个为DEFINER,一个为INVOKER ...

  9. elasticsearch 集群搭建及启动常见错误

    1.系统环境 三台服务器(最好是单数台,跟master选举方式有关),确保机器互相ping的通,且都需要装了jdk 8环境,机器IP和 elasticsearch 的节点名称如下: cluster n ...

  10. SqlServer 高级查询

    高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student;   --all 查询所有 select all sex from ...