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)

题解:直接枚举。注意旋转是说 每个数字单独旋转,并不是把整个数旋转。

  1. class Solution {
  2. public:
  3. int rotatedDigits(int N) {
  4. unordered_map<int, int> mp;
  5. mp[] = , mp[] = , mp[] = , mp[]= , mp[] = , mp[] = , mp[] = ;
  6. int ans = ;
  7. for (int i = ; i <= N; ++i) {
  8. string s = to_string(i);
  9. string rev = "";
  10. bool valid = true;
  11. for (auto c : s) {
  12. if (mp.find(c-'') == mp.end()) {
  13. valid = false;
  14. break;
  15. }
  16. rev += string(, mp[c-''] + '');
  17. }
  18. if (valid && s != rev && rev[] != '') {
  19. ans++;
  20. }
  21. }
  22. return ans;
  23. }
  24. };

【789】Escape The Ghosts(第二题 5分)逃离鬼魂(谷歌tag,数学题)

假设你站在原点,有个去的目标 target 坐标,在二维平面上有一些鬼魂(鬼也有坐标)。每一步,你和鬼魂都能上下左右移动一格。问有没有可能在鬼怪抓到你之前,到达target。如果你和鬼怪同时到达target的话,那么鬼赢了。

题解:我比赛的时候想成了比较复杂的bfs,去模拟每个位置。幸好没有写。本题的本质是只要你和target 的曼哈顿距离小于任何一个鬼怪距离target的目标距离,你就有可能会赢。所以只要判断一下距离就行了。

  1. class Solution {
  2. public:
  3. bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
  4. const int dist = abs(target[]) + abs(target[]);
  5. for (auto& ele : ghosts) {
  6. int d = abs(target[] - ele[]) + abs(target[] - ele[]);
  7. if (d < dist) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. };

比赛的时候理解有问题,我一直以为某个时间人和鬼都必须要移动,不能站着不动。但是总监说不能站着不动的话,这题的难度也没有增加,因为如果有个鬼到target的距离和人到target距离如果差了一步的话,人就能到target,如果差了两步的话,鬼就能先离开,然后再回到target,所以这个时候鬼赢了。

然后还有一个变种,如果鬼不能到target,它最多在target周围四个格子,有点像猫和老鼠那题。

【791】Custom Sort String(第三题 5分)

给了两个字符串 S 和 T,S 定义了一种新的字母顺序,要求把 T 按照 S 定义的顺序排序。

题解:我是先用了一个 hashmap 记录了 T 的每个字母的频次,然后再遍历一遍 S, 构造ans字符串,最后遍历hashmap中剩下的字符,组成新的字符串。

  1. class Solution {
  2. public:
  3. string customSortString(string S, string T) {
  4. const int n = T.size();
  5. unordered_map<char, int> mp;
  6. for (int i = ; i < n; ++i) {
  7. mp[T[i]]++;
  8. }
  9. string ret;
  10. for (auto c : S) {
  11. if (mp.find(c) == mp.end() || mp[c] == ) {continue;}
  12. ret += string(mp[c], c);
  13. mp[c] = ;
  14. }
  15. for (auto ele : mp) {
  16. if (ele.second > ) {
  17. ret += string(ele.second, ele.first);
  18. }
  19. }
  20. return ret;
  21. }
  22. };

【790】Domino and Tromino Tiling(第四题 5分)

给了多米诺骨牌,有两种形状,

  1. XX <- domino
  2.  
  3. XX <- "L" tromino
  4. 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个字符宽度,问给了一个字符串,返回两个数字,一个是这个字符串需要占多少行,第二个是这个字符串的最后一行有多少个字符。

  1. Example :
  2. Input:
  3. 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]
  4. S = "abcdefghijklmnopqrstuvwxyz"
  5. Output: [3, 60]
  6. Explanation:
  7. All letters have the same length of 10. To write all 26 letters,
  8. we need two full lines and one line with 60 units.

题解:用个 curSum 的变量记录当前行有多少个字符,当 curSum 大于 100 的时候就换行。

  1. class Solution {
  2. public:
  3. vector<int> numberOfLines(vector<int>& widths, string S) {
  4. const int n = S.size();
  5. int curLen = ;
  6. vector<int> ans(, );
  7. for (int i = ; i < n; ++i) {
  8. curLen += widths[S[i]-'a'];
  9. if (curLen > ) {
  10. ans[]++;
  11. curLen = widths[S[i]-'a'];
  12. }
  13. }
  14. ans[] += ;
  15. ans[] = curLen;
  16. return ans;
  17. }
  18. };

【804】Unique Morse Code Words(第二题 4分)

给了一个数组作为 'a' 到 'z' 的摩斯码编码,给了一个 word list, 不同的单词可能有相同的莫斯码编码,返回有多少不同的摩斯码编码的种类。

题解:遍历,用set去除重复。

  1. class Solution {
  2. public:
  3. int uniqueMorseRepresentations(vector<string>& words) {
  4. vector<string> morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  5. set<string> st;
  6. for (auto& w : words) {
  7. string str;
  8. for (auto& c : w) {
  9. str += morse[c - 'a'];
  10. }
  11. st.insert(str);
  12. }
  13. return (int)st.size();
  14. }
  15. };

【807】Max Increase to Keep City Skyline(第三题 5分)

给了一个 二维的grid, grid[i][j] 代表 (i, j) 坐标的楼的高度,我们能从前后和左右看到两个高度的轮廓,我们的目标是增加楼的高度,但是让轮廓线不变。

  1. Example:
  2. Input: grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
  3. Output: 35
  4. Explanation:
  5. The grid is:
  6. [ [3, 0, 8, 4],
  7. [2, 4, 5, 7],
  8. [9, 2, 6, 3],
  9. [0, 3, 1, 0] ]
  10.  
  11. The skyline viewed from top or bottom is: [9, 4, 8, 7]
  12. The skyline viewed from left or right is: [8, 7, 9, 3]
  13.  
  14. The grid after increasing the height of buildings without affecting skylines is:
  15.  
  16. gridNew = [ [8, 4, 8, 7],
  17. [7, 4, 7, 7],
  18. [9, 4, 8, 7],
  19. [3, 3, 3, 3] ]

题解:我们先计算出 top 和 left 的轮廓线,grid[i][j] 能取到的最大的高度就是 min(top[j], left[i])

  1. class Solution {
  2. public:
  3. int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
  4. const int n = grid.size(), m = grid[].size();
  5. vector<int> fromTop(m, ), fromLeft(n, );
  6. for (int i = ; i < n; ++i) {
  7. for (int j = ; j < m; ++j) {
  8. fromTop[j] = max(fromTop[j], grid[i][j]);
  9. fromLeft[i] = max(fromLeft[i], grid[i][j]);
  10. }
  11. }
  12. int ret = ;
  13. for (int i = ; i < n; ++i) {
  14. for (int j = ; j < m; ++j) {
  15. ret += min(fromTop[j], fromLeft[i]) - grid[i][j];
  16. }
  17. }
  18. return ret;
  19. }
  20. };

【805】Split Array With Same Average(第四题 9分)

【Leetcode周赛】从contest-71开始。(一般是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-51开始。(一般是10个contest写一篇文章)

    Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...

  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. shiro安全框架学习-1

    1. apche shiro 是Java的一个安全)框架 2.shiro可以非常容易的开发出足够好的应用,不仅可以在JavaSE环境,也可用在JavaEE环境 3. shiro可以完成 认证,授权,加 ...

  2. php strtr()函数 语法

    php strtr()函数 语法 作用:转换字符串中的某些字符直线电机生产厂家 语法:strtr(string,from,to)或者strtr(string,array) 参数: 参数 描述 stri ...

  3. CSS3中哪些新属性—阴影、文本省略(1)

    CSS3中的阴影,我知道的就是盒阴影和文字阴影.两者使用大同小异. 1.文字阴影 不知道为啥阴影会被开发出来,觉得这没啥好用啊.用了之后发现好像还行,使页面更有立体感了那么一点点.看起来趣味性强一点. ...

  4. JQuery触发hover事件无效时使用js原生的触发事件方法

    需求:在开发一个从微信公众号后台管理网页上爬取数据的chrome插件时,有部分页面元素是只显示了部分摘要信息的,需要把鼠标移上去后才能显示全部信息(类似title的弹出显示).这就需要在chrome插 ...

  5. POJ 1066 Treasure Hunt [想法题]

    题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...

  6. ruby的实例变量

    class Box def initialize(w,h) @width,@height=w,h end def getArea @height*@width end end class BigBox ...

  7. 拒绝从入门到放弃_《Openstack 设计与实现》必读目录

    目录 目录 关于这本书 必看知识点 最后 关于这本书 <Openstack 设计与实现>是一本非常值得推荐的书,为数不多的 Openstack 开发向中文书籍中的精品.如果希望从事 Ope ...

  8. 死锁(Deadlock)

    死锁:是指是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程. ...

  9. indy idhttpserver有关下载的两个问题

    http://aawwmate.blog.163.com/blog/static/77528256201092733950315/ indy idhttpserver有关下载的两个问题 2010-10 ...

  10. WPF属性之理解附加属性

    附加属性,顾名思义,和被附加的控件没有依赖关系,只是强行给目标控件挂上一个“属性值”,以便于操作之.就好比,你在学校是学生,那么就要听老师的管教,在公司是下属,就要服从老板的命令一样. 我们常见的附加 ...