当前位置: 首页 > news >正文

leetcode竞赛:20220904周赛

本次题目比较难,体现在中等题比较难。困难题的模拟需要两个堆,思维量也比较大。

题目一 6167. 检查相同字母间的距离

是模拟题,有一定思维量。
比赛代码

class Solution {
public:
    
    bool checkDistances(string s, vector<int>& y) {
        int n = s.size();
        for (int i = 0; i < n; i++) {
            int id = s[i] - 'a';
            int ds = y[id];
            ds += 1;
            bool flag = false;
            
            if (i - ds >= 0 && s[i - ds] == s[i]) {
                flag = true;
            }
            if (i + ds < n && s[i + ds] == s[i]) {
                flag = true;
            }
            if (ds == 0) flag = false;
            if (!flag) {
                return false;
            }
        }
        return true;
    }
};

赛后优化

class Solution {
public:
    
    bool checkDistances(string s, vector<int>& y) {
        int n = s.size();
        vector<int> p[26];
        for (int i = 0; i < n; i++) {
            p[s[i] - 'a'].push_back(i);
        }
        for (int i = 0; i < 26; i++) {
            if (!p[i].size()) continue;
            if (p[i][1] - p[i][0] - 1 != y[i]) return false;
        }
        return true;
    }
};

题目二

比赛时,经验不足还想逆向考虑。实际上看到数据范围,就知道,正向递推一定可做。
比赛代码:数据平移和取模出错了两次,这个不应该,需要反思

class Solution {
public:
    int numberOfWays(int startPos, int endPos, int k) {
        int a = 1003, b = a + abs(startPos - endPos);
        int p = 1e9 + 7;
        vector<vector<int>> dp(1005, vector<int>(2005));
        dp[0][a] = 1;
        for (int i = 1; i <= k; i++) {
            for (int j = 1; j <= 2003; j++) {
                dp[i][j] = dp[i - 1][j - 1] % p  + dp[i - 1][j + 1] % p;
                dp[i][j] %= p;
            }
        }
        return dp[k][b];
    }
};

其他算法:数学+费马小定理


题目三

比赛时,使用了位运算进行优化。实际上不需要。
双指针算法不够熟练,代码写的比较丑。
多插一句:这里为什么可以用双指针?因为两个指针只能往右走。右指针向右的时候,如果左指针可以往左走,那么,右指针在原来位置的时候,左指针就能够在左边的位置。所以左指针位置就是错的,矛盾了。

class Solution {
public:
    int longestNiceSubarray(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n, 1);
        int cur = nums[0], res = 1;
        for (int i = 1, j = 0; i < n; i++) {
            if (cur & nums[i]) {  // 这里应该直接替换成循环
                res = max(res, i - j);
                while (cur & nums[i] && j < i) {
                    cur &= ~nums[j];
                    j++;
                }
                cur |= nums[i];
            } else {
                cur|= nums[i];
                res = max(res, i - j + 1);
            }
        }
        return res;
    }
};

赛后优化

class Solution {
public:
    int longestNiceSubarray(vector<int>& nums) {
        int n = nums.size();
        vector<int> dp(n, 1);
        int cur = nums[0], res = 1;
        for (int i = 1, j = 0; i < n; i++) {
            while (cur & nums[i]) {
                cur &= ~nums[j];
                j++;
            }
            cur |= nums[i];
            res = max(res, i - j + 1);
        }
        return res;
    }
};

第四题:6170. 会议室 III

比赛时没做出来,难点在于数据结构的选择与维护。
这个题要用堆来找到可用的,最小编号的会议室。
要用堆来维护哪些会议室当前可用。当没有可用的时候,需要维护哪个会议室最先使用完。
得想清楚这里有两个要维护的东西,需要两个堆来维护
贴一个

#define x first
#define y second
typedef long long LL;
typedef pair<LL, int> PIL;

class Solution {
public:
    int mostBooked(int n, vector<vector<int>>& meetings) {
        int m = meetings.size();
        priority_queue<int, vector<int>, greater<int>> heap;  // 维护当前可用的会议室编号
        priority_queue<PIL, vector<PIL>, greater<PIL>> rooms; // 维护使用状态的 最早结束 的会议室
        for (int i = 0; i < n; i ++ ) heap.push(i);

        sort(meetings.begin(), meetings.end());
        vector<int> cnt(n);
        for (auto& p: meetings) {
            while (rooms.size() && rooms.top().x <= p[0]) { // 把当前结束会议的会议室空出来
                heap.push(rooms.top().y);
                rooms.pop();
            }
            if (heap.size()) { // 如果有空的会议室,选最小编号的会议室开始开会
                int t = heap.top();
                heap.pop();
                cnt[t] ++ ;
                rooms.push({p[1], t});
            } else { // 没有空的会议室,选最早结束会议的会议室,更新其结束时间。
                auto t = rooms.top();
                rooms.pop();
                cnt[t.y] ++ ;
                rooms.push({t.x + p[1] - p[0], t.y});
            }
        }

        int res = 0;
        for (int i = 1; i < n; i ++ )
            if (cnt[i] > cnt[res])
                res = i;
        return res;
    }
};

相关文章:

  • 算法小讲堂之B树和B+树(浅谈)|考研笔记
  • 【02】Camunda系列-扩展案例-用户任务、网关、决策自动化
  • J. Counting Trees (树,卡特兰数)
  • 77-Java的Set系列集合、Collection体系的总结
  • this指哪去了
  • 算法----二维区域和检索 - 矩阵不可变(Kotlin)
  • 向Visual Studio Code导入ST项目
  • ES6转为ES5 AST
  • 二分法查找方法
  • UE5物体旋转(蓝图版)
  • 【网络安全】SQL注入专题讲解
  • unordered_set、unordered_map的介绍+使用+比较
  • Leetcode139. 单词拆分
  • DRM系列(9)之drm_atomic_helper_commit
  • Unity入门03——Unity脚本
  • JS 中的深拷贝与浅拷贝
  • Angular 响应式表单之下拉框
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  •  D - 粉碎叛乱F - 其他起义
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Java的Interrupt与线程中断
  • markdown编辑器简评
  • vue中实现单选
  • Zsh 开发指南(第十四篇 文件读写)
  • 记录一下第一次使用npm
  • 记一次用 NodeJs 实现模拟登录的思路
  • 聚簇索引和非聚簇索引
  • 通过git安装npm私有模块
  • 突破自己的技术思维
  • 以太坊客户端Geth命令参数详解
  • Mac 上flink的安装与启动
  • 数据可视化之下发图实践
  • #if 1...#endif
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (26)4.7 字符函数和字符串函数
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (TOJ2804)Even? Odd?
  • (二)PySpark3:SparkSQL编程
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (十六)串口UART
  • (五)c52学习之旅-静态数码管
  • (转)3D模板阴影原理
  • (转)母版页和相对路径
  • (转)人的集合论——移山之道
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .Net Web项目创建比较不错的参考文章
  • .NET连接数据库方式
  • ::
  • @Autowired和@Resource的区别
  • @Transient注解
  • @Validated和@Valid校验参数区别
  • [ Linux 长征路第五篇 ] make/Makefile Linux项目自动化创建工具