力扣第357场周赛补题

6925. 故障键盘 - 力扣(LeetCode)

思路:模拟

cpp 复制代码
class Solution {
public:
    string finalString(string s) {
        string res;
        for(auto c : s)
        {
            if(c == 'i') reverse(res.begin(), res.end());
            else res += c;
        }
        
        return res;
    }
};

6953. 判断是否能拆分数组 - 力扣(LeetCode)

思路:脑筋急转弯

cpp 复制代码
class Solution {
public:
    bool canSplitArray(vector<int>& nums, int m) {
        int n = nums.size();
        if(n <= 2) return true;
        for(int i = 0; i < n - 1; i ++ )
            if(nums[i] + nums[i + 1] >= m) 
                return true;
        return false;
    }
};

2812. 找出最安全路径 - 力扣(LeetCode)

思路:多源bfs+倒序枚举+并查集

cpp 复制代码
class Solution {
public:
    int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    typedef pair<int, int> PII;
    int maximumSafenessFactor(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<PII> q;
        vector<vector<int>> dist(n, vector<int>(n, -1));
        for(int i = 0; i < n; i ++ )
            for(int j = 0; j < n; j ++ )
                if(grid[i][j])
                {
                    q.emplace_back(i, j);
                    dist[i][j] = 0;
                }
        vector<vector<PII>> groups = {q};//groups每一层就代表的到1的距离相同的点
        //第0层就是1那些点,然后第一层就是第一层遍历的点,以此类推
        while (q.size())
        {
            vector<PII> nq;
            for(auto &[i, j] : q)
            {
                for (int k = 0; k < 4; k ++ ) 
                {
                    int x = i + dx[k], y = j + dy[k];
                    if(x >= 0 && x < n && y >= 0 && y < n && dist[x][y] < 0)
                    {
                        nq.emplace_back(x, y);
                        dist[x][y] = groups.size();
                    }
                }
            }
            groups.push_back(nq);
            q = move(nq);//move将nq强制转化为右值引用
        }
        //并查集
        vector<int> p(n * n);
        iota(p.begin(), p.end(), 0);//0,1,2,3,4...
        function<int(int)> find = [&](int x) -> int {
            if(p[x] != x) return p[x] = find(p[x]); 
            return p[x];
        };
        //因为最后还加了一层空数组,所以减2
        for(int res = groups.size() - 2; res > 0; res -- )
        {
            for(auto &[i, j] : groups[res])
            {
                for (int k = 0; k < 4; k ++ ) 
                {
                    int x = i + dx[k], y = j + dy[k];
                    if(x >= 0 && x < n && y >= 0 && y < n && dist[x][y] >= dist[i][j])
                        p[find(x * n + y)] = find(i * n + j);
                }
            }
            if(find(0) == find(n * n - 1)) return res;
        }
        return 0;
    }
};
相关推荐
再睡一夏就好几秒前
深入解析Linux页表:从虚拟地址到物理内存的映射艺术
linux·运维·服务器·c语言·c++·页表·缺页异常
白兰地空瓶6 分钟前
你以为树只是画图?不——它是算法面试的“隐形主角”
前端·javascript·算法
柒.梧.14 分钟前
数据结构:二叉排序树的删除操作实现
数据结构
好易学·数据结构16 分钟前
可视化图解算法74:最小花费爬楼梯
数据结构·算法·leetcode·动态规划·力扣
Starry_hello world26 分钟前
C++ 线程 (3)
c++
Maỿbe41 分钟前
力扣hot图论部分
算法·leetcode·图论
雍凉明月夜44 分钟前
c++ 精学笔记记录Ⅲ
c++·笔记·学习
LYFlied1 小时前
【每日算法】LeetCode 78. 子集
数据结构·算法·leetcode·面试·职场和发展
月明长歌1 小时前
【码道初阶】【Leetcode606】二叉树转字符串:前序遍历 + 括号精简规则,一次递归搞定
java·数据结构·算法·leetcode·二叉树
子枫秋月1 小时前
C++字符串操作与迭代器解析
数据结构·算法