leetcode 403. 青蛙过河

题目:403. 青蛙过河 - 力扣(LeetCode)

O(n^2)水题

cpp 复制代码
class Solution {
public:
    bool canCross(vector<int>& stones) {
        int n = (int) stones.size();
        vector<vector<int>> f;
        f.resize(n);
        f[0].push_back(1);
        int64_t temp;
        for (int i = 0; i < n - 1; i++) {
            vector<int>& t = f[i];
            sort(t.begin(), t.end());
            int j = i + 1;
            for (int k = 0; k < t.size(); k++) {
                if (k > 0 && t[k] == t[k - 1]) {
                    continue;
                }
                temp = stones[i];
                temp += t[k];
                if (temp > stones[j]) {
                    while (j < n - 1 && temp >= stones[j + 1]) {
                        j++;
                    }
                }
                if (stones[i] + t[k] == stones[j]) {
                    if (j == n - 1) {
                        return true;
                    }
                    if (t[k] > 1) {
                        f[j].push_back(t[k] - 1);
                    }
                    f[j].push_back(t[k]);
                    f[j].push_back(t[k] + 1);
                    continue;
                }
            }
        }
        return false;
    }
};
相关推荐
菜菜的顾清寒2 小时前
力扣HOT100(42)链表-随机链表的复制
算法·leetcode·链表
菜菜的顾清寒5 小时前
HOT力扣100(43)二叉树-翻转二叉树
数据结构·算法·leetcode
csdn_aspnet7 小时前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
Navigator_Z7 小时前
LeetCode //C - 1073. Adding Two Negabinary Numbers
c语言·算法·leetcode
csdn_aspnet7 小时前
PHP 算法 LeetCode 编号 70 - 爬楼梯
算法·leetcode·php
x_xbx8 小时前
LeetCode:5. 最长回文子串
算法·leetcode·职场和发展
小欣加油11 小时前
leetcode 3300 替换为数位和后的最小元素
数据结构·c++·算法·leetcode
人道领域12 小时前
【LeetCode刷题日记】108.将有序数组转换为二叉搜索树
java·算法·leetcode
过期动态12 小时前
【LeetCode 热题 100】无重复字符的最长子串
java·数据结构·spring boot·算法·leetcode·职场和发展
莫等闲-13 小时前
leetcode42. 接雨水 leetcode84.柱状图中最大的矩形
数据结构·c++·算法·leetcode