【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)
Contest 71 ()
Contest 72 ()
Contest 73 (2019年1月30日模拟)
链接:https://leetcode.com/contest/weekly-contest-73
结果:2/4,会做第一题和第三题,第二题和第四题不会做。
【788】Rotated Digits(第一题 4分)(谷歌tag)
给了一个 good number 的定义,X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X.
0, 1, 8旋转之后是本身,2,5旋转之后互为另一个数,6,9 同 2,5。
给定一个 N,问 1~N 里面有多少个 good number?(N < 10000)
题解:直接枚举。注意旋转是说 每个数字单独旋转,并不是把整个数旋转。
class Solution {
public:
int rotatedDigits(int N) {
unordered_map<int, int> mp;
mp[] = , mp[] = , mp[] = , mp[]= , mp[] = , mp[] = , mp[] = ;
int ans = ;
for (int i = ; i <= N; ++i) {
string s = to_string(i);
string rev = "";
bool valid = true;
for (auto c : s) {
if (mp.find(c-'') == mp.end()) {
valid = false;
break;
}
rev += string(, mp[c-''] + '');
}
if (valid && s != rev && rev[] != '') {
ans++;
}
}
return ans;
}
};
【789】Escape The Ghosts(第二题 5分)逃离鬼魂(谷歌tag,数学题)
假设你站在原点,有个去的目标 target 坐标,在二维平面上有一些鬼魂(鬼也有坐标)。每一步,你和鬼魂都能上下左右移动一格。问有没有可能在鬼怪抓到你之前,到达target。如果你和鬼怪同时到达target的话,那么鬼赢了。
题解:我比赛的时候想成了比较复杂的bfs,去模拟每个位置。幸好没有写。本题的本质是只要你和target 的曼哈顿距离小于任何一个鬼怪距离target的目标距离,你就有可能会赢。所以只要判断一下距离就行了。
class Solution {
public:
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
const int dist = abs(target[]) + abs(target[]);
for (auto& ele : ghosts) {
int d = abs(target[] - ele[]) + abs(target[] - ele[]);
if (d < dist) {
return false;
}
}
return true;
}
};
比赛的时候理解有问题,我一直以为某个时间人和鬼都必须要移动,不能站着不动。但是总监说不能站着不动的话,这题的难度也没有增加,因为如果有个鬼到target的距离和人到target距离如果差了一步的话,人就能到target,如果差了两步的话,鬼就能先离开,然后再回到target,所以这个时候鬼赢了。
然后还有一个变种,如果鬼不能到target,它最多在target周围四个格子,有点像猫和老鼠那题。
【791】Custom Sort String(第三题 5分)
给了两个字符串 S 和 T,S 定义了一种新的字母顺序,要求把 T 按照 S 定义的顺序排序。
题解:我是先用了一个 hashmap 记录了 T 的每个字母的频次,然后再遍历一遍 S, 构造ans字符串,最后遍历hashmap中剩下的字符,组成新的字符串。
class Solution {
public:
string customSortString(string S, string T) {
const int n = T.size();
unordered_map<char, int> mp;
for (int i = ; i < n; ++i) {
mp[T[i]]++;
}
string ret;
for (auto c : S) {
if (mp.find(c) == mp.end() || mp[c] == ) {continue;}
ret += string(mp[c], c);
mp[c] = ;
}
for (auto ele : mp) {
if (ele.second > ) {
ret += string(ele.second, ele.first);
}
}
return ret;
}
};
【790】Domino and Tromino Tiling(第四题 5分)
给了多米诺骨牌,有两种形状,
XX <- domino XX <- "L" tromino
X
给定一个 N 问有多少种填充方案能填满 2 * N 个格子。答案要对 1e9+7 取模。
题解:dp做。
Contest 74 (2019年1月31日模拟)
链接:https://leetcode.com/contest/weekly-contest-74
Contest 75 (2019年1月31日模拟)
链接:https://leetcode.com/contest/weekly-contest-75
Contest 77 (2019年2月3日模拟)
链接:https://leetcode.com/contest/weekly-contest-77
总结:做出来三道题。3/4
【806】Number of Lines To Write String(第一题 4分)
给了一个数组,数组中的元素作为 'a' ~ 'z' 的每个占位符的宽度。每行最多100个字符宽度,问给了一个字符串,返回两个数字,一个是这个字符串需要占多少行,第二个是这个字符串的最后一行有多少个字符。
Example :
Input:
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
Output: [3, 60]
Explanation:
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.
题解:用个 curSum 的变量记录当前行有多少个字符,当 curSum 大于 100 的时候就换行。
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
const int n = S.size();
int curLen = ;
vector<int> ans(, );
for (int i = ; i < n; ++i) {
curLen += widths[S[i]-'a'];
if (curLen > ) {
ans[]++;
curLen = widths[S[i]-'a'];
}
}
ans[] += ;
ans[] = curLen;
return ans;
}
};
【804】Unique Morse Code Words(第二题 4分)
给了一个数组作为 'a' 到 'z' 的摩斯码编码,给了一个 word list, 不同的单词可能有相同的莫斯码编码,返回有多少不同的摩斯码编码的种类。
题解:遍历,用set去除重复。
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
set<string> st;
for (auto& w : words) {
string str;
for (auto& c : w) {
str += morse[c - 'a'];
}
st.insert(str);
}
return (int)st.size();
}
};
【807】Max Increase to Keep City Skyline(第三题 5分)
给了一个 二维的grid, grid[i][j] 代表 (i, j) 坐标的楼的高度,我们能从前后和左右看到两个高度的轮廓,我们的目标是增加楼的高度,但是让轮廓线不变。
Example:
Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
Output: 35
Explanation:
The grid is:
[ [3, 0, 8, 4],
[2, 4, 5, 7],
[9, 2, 6, 3],
[0, 3, 1, 0] ] The skyline viewed from top or bottom is: [9, 4, 8, 7]
The skyline viewed from left or right is: [8, 7, 9, 3] The grid after increasing the height of buildings without affecting skylines is: gridNew = [ [8, 4, 8, 7],
[7, 4, 7, 7],
[9, 4, 8, 7],
[3, 3, 3, 3] ]
题解:我们先计算出 top 和 left 的轮廓线,grid[i][j] 能取到的最大的高度就是 min(top[j], left[i])
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
const int n = grid.size(), m = grid[].size();
vector<int> fromTop(m, ), fromLeft(n, );
for (int i = ; i < n; ++i) {
for (int j = ; j < m; ++j) {
fromTop[j] = max(fromTop[j], grid[i][j]);
fromLeft[i] = max(fromLeft[i], grid[i][j]);
}
}
int ret = ;
for (int i = ; i < n; ++i) {
for (int j = ; j < m; ++j) {
ret += min(fromTop[j], fromLeft[i]) - grid[i][j];
}
}
return ret;
}
};
【805】Split Array With Same Average(第四题 9分)
【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)的更多相关文章
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- 【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)
注意,以前的比赛我是自己开了 virtual contest.这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力. 前面已经有了100个contest.计划是每周做三个到五个cont ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)
Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...
- 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)
Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
[LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...
- 【Leetcode周赛】比赛目录索引
contest 1 ~ contest 10: contest 11 ~ contest 20: contest 21 ~ contest 30 : https://www.cnblogs.com/z ...
随机推荐
- service pom
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- 变量类型,-数据类型(值类型,引用类型)uint 不有存负数,int,可以存负数,
俩种命名方法 1.Pascal 命名法,第一个字母大写其它字母小写Userid 2.Camel命名法,所有单第一方写大写,其它小写,骆峰命名法,userId 程序中元素的命名规范项目名:公司名.项目名 ...
- python 全栈开发,Day45(html介绍和head标签,body标签中相关标签)
一.html介绍 1.web标准 web准备介绍: w3c:万维网联盟组织,用来指定web标准的机构(组织) web标准:制作网页遵循的规范 web准备规范的分类:结构标准.表现标准.行为标准. 结构 ...
- php实现进度条原理
PHP实现进度条的原理: 模版替换,在页面设置一个标识,轮子自己的页面,不发请求给服务器,由服务器端获得进度,然后替换该页面标识,达到进度条效果. 页面代码: 1 2 3 4 5 6 7 8 9 10 ...
- 862C - Mahmoud and Ehab and the xor(构造)
原题链接:http://codeforces.com/contest/862/problem/C 题意:给出n,x,求n个不同的数,使这些数的异或和为x 思路:(官方题解)只有n==2&&am ...
- JS对象、原型、this学习总结
1.对象是函数创建的,而函数却又是一种对象.(属性的集合) 2.每个函数都有一个属性叫做prototype.这个prototype的属性值是一个对象,默认的只有一个constructor的属性,指向这 ...
- UVA 1045 最长公共子序列
题目描述:求最长公共子序列 若给定序列X={x1,x2,...,xm},另一序列Z={z1,z2,...,zk},是X的子序列是指存在一个严格递增的下标序列{i1,i2,...,ik}使得对所以j=1 ...
- elasticsearch 5.1 认证过期 (your license has expired)
首先说一下License过期后的状况: if 设置了登录认证,license过期后将无法登录(无法填入用户名密码,下方给出报错,license过期): if 没有设置登录认证,打开kibaba界面中M ...
- 从零开始创建 symfony-cmf
前提: 官方 https://symfony.com/doc/master/cmf/quick_tour/the_big_picture.html#setting-up-the-database 由于 ...
- UCenter 与 DIscuz 通信失败的解决方法
问题状况:Discuz 用户无法成功修改头像且帖子中上传的图片无法保存.进入 Discuz 后台检查,一切正常:进入 UCenter 检查后发现在"应用管理"中与 Discuz 论 ...