Bootstrap

leetcode 403. 青蛙过河

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

O(n^2)水题

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;
    }
};

;