GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/055_Jump_Game.java at master · xinyyyhu/leetcode Expand file tree/ Copy path055_Jump_Game.javaMore file actions 49 lines (40 loc) · 1.39 KB / Copy path055_Jump_Game.javaFile metadata and controls49 lines (40 loc) · 1.39 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import java.util.*; class Solution { public boolean canJump(int[] nums) { /* * Constraints * 1 <= nums.length <= 10^4 * 0 <= nums[i] <= 10^5 * * Solution * 1. Use BFS Algorithm. * - reason 1 : have to ignore array which is not visited. * - reason 2 : we have to visit all possible array from array[start]. */ int N = nums.length; ArrayDeque<Integer> q = new ArrayDeque<>(); boolean[] visited = new boolean[N]; // First, add first array index. // And, set visited[first_index] to true. q.add(0); visited[0] = true; // Axiom : if N is 1, result is true. if(N == 1) return true; // BFS algorithm while(!q.isEmpty()) { int cur = q.poll(); // find cur + 1 to cur + nums[cur] for(int i = 1; i <= nums[cur]; i++) { if(cur + i >= N - 1) return true; int next = Math.min(cur + i, N - 1); // set visited[next] to true and add index into queue. // because of time limit(No overlap steps.) if(!visited[next]) { visited[next] = true; q.add(next); } } } return false; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |