Skip to content

Commit 071b5f6

Browse files
add 1345
1 parent 6934c61 commit 071b5f6

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1345|[Jump Game IV](https://leetcode.com/problems/jump-game-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1345.java) | |Hard|BFS|
1112
|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1344.java) | |Medium|Math|
1213
|1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium|Array|
1314
|1342|[Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Easy|Bit Manipulation|
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
9+
/**
10+
* 1345. Jump Game IV
11+
*
12+
* Given an array of integers arr, you are initially positioned at the first index of the array.
13+
* In one step you can jump from index i to index:
14+
* i + 1 where: i + 1 < arr.length.
15+
* i - 1 where: i - 1 >= 0.
16+
* j where: arr[i] == arr[j] and i != j.
17+
* Return the minimum number of steps to reach the last index of the array.
18+
* Notice that you can not jump outside of the array at any time.
19+
*
20+
* Example 1:
21+
* Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
22+
* Output: 3
23+
* Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
24+
*
25+
* Example 2:
26+
* Input: arr = [7]
27+
* Output: 0
28+
* Explanation: Start index is the last index. You don't need to jump.
29+
*
30+
* Example 3:
31+
* Input: arr = [7,6,9,6,9,6,9,7]
32+
* Output: 1
33+
* Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
34+
*
35+
* Example 4:
36+
* Input: arr = [6,1,9]
37+
* Output: 2
38+
*
39+
* Example 5:
40+
* Input: arr = [11,22,7,7,7,7,7,7,7,22,13]
41+
* Output: 3
42+
*
43+
* Constraints:
44+
* 1 <= arr.length <= 5 * 10^4
45+
* -10^8 <= arr[i] <= 10^8
46+
* */
47+
public class _1345 {
48+
public static class Solution1 {
49+
/**credit: https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC%2B%2B-BFS-Solution-Clean-code-O(N)*/
50+
public int minJumps(int[] arr) {
51+
Map<Integer, List<Integer>> valueToIndices = new HashMap<>();
52+
for (int i = 0; i < arr.length; i++) {
53+
valueToIndices.computeIfAbsent(arr[i], k -> new LinkedList<>()).add(i);
54+
}
55+
boolean[] visited = new boolean[arr.length];
56+
Queue<Integer> indexQueue = new LinkedList<>();
57+
indexQueue.offer(0);
58+
int steps = 0;
59+
while (!indexQueue.isEmpty()) {
60+
int size = indexQueue.size();
61+
for (int i = 0; i < size; i++) {
62+
int index = indexQueue.poll();
63+
if (index == arr.length - 1) {
64+
return steps;
65+
}
66+
List<Integer> nextPossibleIndices = valueToIndices.get(arr[index]);
67+
nextPossibleIndices.add(index - 1);
68+
nextPossibleIndices.add(index + 1);
69+
for (int next : nextPossibleIndices) {
70+
if (next >= 0 && next < arr.length && !visited[next]) {
71+
visited[next] = true;
72+
indexQueue.offer(next);
73+
}
74+
}
75+
nextPossibleIndices.clear();//this line is the key to this entire algorithm to avoid TLE, explanation: https://leetcode.com/problems/jump-game-iv/discuss/502699/JavaC++-BFS-Solution-Clean-code-O(N)/445620
76+
}
77+
steps++;
78+
}
79+
return 0;
80+
}
81+
}
82+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1345;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1345Test {
10+
private static _1345.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1345.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{100, -23, -23, 404, 100, 23, 23, 23, 3, 404};
21+
assertEquals(3, solution1.minJumps(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{7};
27+
assertEquals(0, solution1.minJumps(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{7, 6, 9, 6, 9, 6, 9, 7};
33+
assertEquals(1, solution1.minJumps(arr));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
arr = new int[]{6, 1, 9};
39+
assertEquals(2, solution1.minJumps(arr));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
arr = new int[]{11, 22, 7, 7, 7, 7, 7, 7, 7, 22, 13};
45+
assertEquals(3, solution1.minJumps(arr));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
arr = new int[]{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 11};
51+
assertEquals(2, solution1.minJumps(arr));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)