Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bfec59f

Browse files
add 656
1 parent e5052e4 commit bfec59f

File tree

4 files changed

+130
-8
lines changed

4 files changed

+130
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Your ideas/fixes/algorithms are more than welcome!
2626
|659|[Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_659.java) | O(n) | O(n) | Medium | HashMap
2727
|658|[Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_658.java) | O(n) | O(1) | Medium |
2828
|657|[Judge Route Circle](https://leetcode.com/problems/judge-route-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_657.java) | O(n) | O(1) | Easy |
29+
|656|[Coin Path](https://leetcode.com/problems/coin-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_656.java) | O(n*B) | O(n) | Hard | DP
2930
|654|[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_654.java) | O(n^2) | O(n) | Medium | Tree
3031
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_653.java) | | | Easy | Tree
3132
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_652.java) | O(n) |O(n) | Medium | Tree

pom.xml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
<groupId>com.fishercoder</groupId>
88
<artifactId>leetcode-algorithms</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
1011
<build>
1112
<plugins>
13+
1214
<plugin>
1315
<groupId>org.apache.maven.plugins</groupId>
1416
<artifactId>maven-compiler-plugin</artifactId>
@@ -17,6 +19,26 @@
1719
<target>1.8</target>
1820
</configuration>
1921
</plugin>
22+
23+
<!--<plugin>-->
24+
<!--<groupId>org.apache.maven.plugins</groupId>-->
25+
<!--<artifactId>maven-checkstyle-plugin</artifactId>-->
26+
<!--<version>2.9.1</version>-->
27+
<!--<executions>-->
28+
<!--<execution>-->
29+
<!--<id>checkstyle</id>-->
30+
<!--<phase>validate</phase>-->
31+
<!--<goals>-->
32+
<!--<goal>check</goal>-->
33+
<!--</goals>-->
34+
<!--<configuration>-->
35+
<!--<configLocation>fishercoder_codestyle.xml</configLocation>-->
36+
<!--&lt;!&ndash;<failOnViolation>true</failOnViolation>&ndash;&gt;-->
37+
<!--</configuration>-->
38+
<!--</execution>-->
39+
<!--</executions>-->
40+
<!--</plugin>-->
41+
2042
</plugins>
2143
</build>
2244

@@ -34,14 +56,12 @@
3456
<plugin>
3557
<groupId>org.apache.maven.plugins</groupId>
3658
<artifactId>maven-checkstyle-plugin</artifactId>
37-
<version>2.17</version>
38-
<reportSets>
39-
<reportSet>
40-
<reports>
41-
<report>checkstyle</report>
42-
</reports>
43-
</reportSet>
44-
</reportSets>
59+
<version>2.9.1</version>
60+
<configuration>
61+
<failOnViolation>true</failOnViolation>
62+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
63+
<configLocation>${project.basedir}/fishercoder_codestyle.xml</configLocation>
64+
</configuration>
4565
</plugin>
4666
</plugins>
4767
</reporting>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 656. Coin Path
7+
*
8+
* Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B.
9+
* The integer B denotes that from any place (suppose the index is i) in the array A,
10+
* you can jump to any one of the place in the array A indexed i+1, i+2, …, i+B if this place can be jumped to.
11+
* Also, if you step on the index i, you have to pay Ai coins. If Ai is -1, it means you can’t jump to the place indexed i in the array.
12+
* Now, you start from the place indexed 1 in the array A,
13+
* and your aim is to reach the place indexed N using the minimum coins.
14+
* You need to return the path of indexes (starting from 1 to N) in the array you should take to get to the place indexed N using minimum coins.
15+
* If there are multiple paths with the same cost, return the lexicographically smallest such path.
16+
* If it's not possible to reach the place indexed N then you need to return an empty array.
17+
18+
Example 1:
19+
Input: [1,2,4,-1,2], 2
20+
Output: [1,3,5]
21+
22+
Example 2:
23+
Input: [1,2,4,-1,2], 1
24+
Output: []
25+
26+
Note:
27+
Path Pa1, Pa2, ..., Pan is lexicographically smaller than Pb1, Pb2, ..., Pbm,
28+
if and only if at the first i where Pai and Pbi differ, Pai < Pbi; when no such i exists, then n < m.
29+
A1 >= 0. A2, ..., AN (if exist) will in the range of [-1, 100].
30+
Length of A is in the range of [1, 1000].
31+
B is in the range of [1, 100].
32+
*/
33+
34+
public class _656 {
35+
36+
/**
37+
* Time: O(n*B)
38+
* Reference: https://leetcode.com/articles/coin-path/#approach-3-using-dynamic-programming-accepted
39+
*/
40+
public List<Integer> cheapestJump(int[] A, int B) {
41+
int[] next = new int[A.length];
42+
long[] dp = new long[A.length];
43+
Arrays.fill(next, -1);
44+
List<Integer> res = new ArrayList();
45+
for (int i = A.length - 2; i >= 0; i--) {
46+
long min_cost = Integer.MAX_VALUE;
47+
for (int j = i + 1; j <= i + B && j < A.length; j++) {
48+
if (A[j] >= 0) {
49+
long cost = A[i] + dp[j];
50+
if (cost < min_cost) {
51+
min_cost = cost;
52+
next[i] = j;
53+
}
54+
}
55+
}
56+
dp[i] = min_cost;
57+
}
58+
int i;
59+
for (i = 0; i < A.length && next[i] > 0; i = next[i]) {
60+
res.add(i + 1);
61+
}
62+
if (i == A.length - 1 && A[i] >= 0) {
63+
res.add(A.length);
64+
} else {
65+
return new ArrayList<>();
66+
}
67+
return res;
68+
}
69+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._656;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
/**
14+
* Created by fishercoder on 5/25/17.
15+
*/
16+
public class _656Test {
17+
private static _656 test;
18+
private static int[] A;
19+
private static List<Integer> expected;
20+
21+
@BeforeClass
22+
public static void setup(){
23+
test = new _656();
24+
}
25+
26+
@Test
27+
public void test1(){
28+
A = new int[]{1,2,4,-1,2};
29+
expected = new ArrayList<>(Arrays.asList(1,3,5));
30+
assertEquals(expected, test.cheapestJump(A, 2));
31+
}
32+
}

0 commit comments

Comments
 (0)