跳跃游戏

题目出处:https://leetcode-cn.com/problems/jump-game-vii/
在这里插入图片描述
方法一:递归方法(超时)

class Solution {
private:
    bool flag = false;            
public:
    void dfs(int start, vector<bool>& vis, string &s, int& minJump, int& maxJump){
        int nextl = start + minJump;
        int nextr = start + maxJump;
        if(nextl <= s.length()-1 && nextr >= s.length()-1 && s[s.length()-1] == '0'){
            flag = true;
            return;
        }
            
        for(int g = nextl; g <= min(nextr, (int)(s.length()-1)) && !flag; ++g){
            if(s[g] == '0' && !vis[g]){
                vis[g] = true;
                dfs(g, vis, s, minJump, maxJump);
                vis[g] = false;
            }
        }         
    }
    bool canReach(string s, int minJump, int maxJump) {
        if(s[s.length()-1] == '1'){
            return false;
        }
        vector<bool> vis(s.length(), false);
        dfs(0, vis, s, minJump,  maxJump);
        
        return flag;
    }
};

方法二:树状数组

inline int lowbit(int x) {
    return x & (-x);
}

class Solution {
public:
    bool canReach(string s, int minJump, int maxJump) {        
        int n = s.size();
        if (s[n - 1] == '1')
            return false;
        
        vector<int> zero;
        for (int i = 0; i < n; ++i)
            if (s[i] == '0')
                zero.emplace_back(i);
        int m = zero.size();
        
        vector<int> a(m + 1);
        auto query = [&](int pos) {
            int ans = 0;
            for (; pos; pos -= lowbit(pos))
                ans += a[pos];
            return ans;
        };
        auto update = [&](int pos, int val) {
            for (; pos <= m; pos += lowbit(pos))
                a[pos] += val;
        };

        update(1, 1);
        update(2, -1);
        int l = 0, r = 0;
        for (int i = 1; i < m; ++i) {
            if (query(i) > 0) {
                while (l < m && zero[l] < zero[i - 1] + minJump)
                    l++;
                while (r < m && zero[r] <= zero[i - 1] + maxJump)
                    r++;
                update(l + 1, 1), update(r + 1, -1);
            }
        }
        
        return query(m) > 0;
    }
};

方法三:平衡二叉搜索树
思路:用有序集合维护所有为0的位置。将已经可以额到达的位置从集合中删除。

class Solution {
public:
    bool canReach(string s, int minJump, int maxJump) {        
        int n = s.size();
        if (s[n - 1] == '1')
            return false;
        
        vector<int> zero;
        for (int i = 0; i < n; ++i)
            if (s[i] == '0')
                zero.emplace_back(i);
        int m = zero.size();
        vector<bool> can(n);
        can[0] = true;
        set<int> st(zero.begin(), zero.end());
        for (int i = 0; i + 1 < m; ++i) {
            if (can[zero[i]]) {
                vector<int> destinations;
                for (auto it = st.lower_bound(zero[i] + minJump); it != st.upper_bound(zero[i] + maxJump); ++it) {
                    destinations.emplace_back(*it);
                }
                for (int destination : destinations) {
                    can[destination] = true;
                    st.erase(destination);
                }
            }
        }
        
        return can[n - 1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值