力扣刷题----跳跃游戏

跳跃游戏1

//1、暴力dp:理所当然,分数不高
     public boolean canJump(int[] nums) {
        //暴力dp
        boolean[] dp = new boolean[nums.length];
        dp[0] = true;
        for (int i = 0; i < nums.length; i++) {
            if (!dp[i]) return false;
            for (int j = i + 1; j <= Math.min(i + nums[i],nums.length - 1); j++) {
                dp[j] = true;
            }
        }
        return dp[nums.length - 1];
    }

```java
 public boolean canJump(int[] nums) {
      if(nums.length == 1){
          return true;
      }
       //思路转换:不断的更新可以跳出的最远距离
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (max >= i) {
                max = Math.max(max, i + nums[i]);
            }
            if (max >= nums.length - 1) return true;
        }
        return false;
    }

跳跃游戏2

    public int jump(int[] nums) {
//贪心:将次数作为for循环的参数,并更新每次所能走的最远距离
        int max = nums[0];
        int pre = 0;
        int max1 = 0;

        for (int j = 1; j < nums.length; j++) {
            if (max >= nums.length - 1) return j;

            //j记录的是次数,max记录每次能到达的最远距离
            pre = max1;
            max1 = max;
            for (int i = pre; i <= max1; i++) {
                max = Math.max(max, i + nums[i]);
            }

        }
        return 0;
    }

跳跃游戏3

//这种题不要先想着dp,明明可以dfs和bfs的
    public boolean canReach(int[] arr, int start) {
        int len=arr.length;
        boolean[] dp=new boolean[len];
        Queue<Integer> queue=new LinkedList<>();
        queue.add(start);
        while(!queue.isEmpty()){
            int x=queue.poll();
            if(x+arr[x]<len&&!dp[x+arr[x]]){
                dp[x+arr[x]]=true;
                queue.add(x+arr[x]);
                if(arr[x+arr[x]]==0)return true;
            }
            if(x-arr[x]>=0&&!dp[x-arr[x]]){
                dp[x-arr[x]]=true;
                queue.add(x-arr[x]);
                if(arr[x-arr[x]]==0)return true;
            }
        }
        return false;
    }

跳跃游戏4

class Solution {
public int maxJumps(int[] arr, int d) {
//首先构造一个优先队列,按照value值从小到大排序
        PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if (o1.value == o2.value){
                    return o1.position - o2.position;
                }
                return o1.value - o2.value;
            }
        });
        for (int i = 0; i < arr.length; i++) {
            Node node = new Node(arr[i],i);
            queue.offer(node);
        }
//动态规划,可以跳的次数,就是周围数+1中的max
        int[] dp =new int[arr.length];
        int max = 0;
        while (!queue.isEmpty()){
            Node poll = queue.poll();
            //寻找周围比自己小的数
            //向左
            for (int i = 1; i <=  d; i++) {
                if (poll.position - i >= 0){
                   if( arr[poll.position - i ] < arr[poll.position]){
                         dp[poll.position] = Math.max(dp[poll.position - i ] + 1,dp[poll.position]);
                    }else {
                       break;//屏障跳出循环
                   }
                }
            }
            for (int i = 1; i <= d; i++) {
                if (i + poll.position < arr.length){
                    if( arr[i + poll.position] < arr[poll.position]){
                        dp[poll.position] = Math.max(dp[i + poll.position] + 1,dp[poll.position]);
                    }else {
                        break;
                    }
                }
            }
            max = Math.max(max,dp[poll.position]);
        }
        return max + 1;
    }
}
class Node{
    int value;
    int position;
    Node(){}
    Node(int value,int position){
        this.value = value;
        this.position = position;
    }
}

跳跃游戏5

 public int minJumps(int[] arr) {
        if (arr.length <= 1)return 0;

        //首先统计,value值为优先队列,数据不强,所以优先队列可以过
        Map<Integer,PriorityQueue<Integer>> map = new HashMap<>();

        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(arr[i])){
                PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2 - o1;
                    }
                });
                queue.add(i);
                map.put(arr[i],queue);
            }else {
                PriorityQueue<Integer> queue = map.get(arr[i]);
                queue.add(i);
            }

        }

        //搞一个队列
        Queue<Integer> queue = new LinkedList<>();
        queue.add(0);
        int count = 0;
        int[] vis = new int[arr.length];
        vis[0] = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int poll = queue.poll();
                if (poll == arr.length - 1) return count;
                if (poll + 1 < arr.length && vis[poll + 1] != 1) {
                    queue.offer(poll + 1);
                    vis[poll + 1] = 1;
                }
                if (poll - 1 >= 0 && vis[poll - 1] != 1) {
                    queue.offer(poll - 1);
                    vis[poll - 1] = 1;
                }
                PriorityQueue<Integer> queue1 = map.get(arr[poll]);
                while (!queue1.isEmpty()) {
                    int p = queue1.poll();
                    if (vis[p] != 1) {
                        queue.offer(p);
                        vis[p] = 1;
                    }
                }
            }
            count++;
        }
        return count;
    }

跳跃游戏6

 public int maxResult(int[] nums, int k) {
//动规+优先队列
        PriorityQueue<int[]> queue = new PriorityQueue<>(((o1, o2) -> o2[0] - o1[0]));
        queue.add(new int[]{nums[0],0});
        int res = nums[0];
        for (int i = 1; i < nums.length; i++) {
            while(i - queue.peek()[1] > k){
                queue.poll();
            }
            res = queue.peek()[0] + nums[i];
            queue.add(new int[]{res,i});
        }

        return res;
    }

//  public int maxResult(int[] nums, int k) {
// //动态规划;超时
//         if (nums.length == 1) return nums[0];
//         int[] dp = new int[nums.length];
//         Arrays.fill(dp,Integer.MIN_VALUE);
//         dp[0] = nums[0];
//         for (int i = 1; i < nums.length; i++) {
//             for (int j = 1; j <= k; j++) {
//                 if (i - j >= 0){
//                     dp[i] = Math.max(dp[i - j] + nums[i],dp[i]);
//                 }
//             }
//         }
//         return dp[dp.length - 1];
//     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值