Skip to content

Commit 538d103

Browse files
refactor 134
1 parent c1c782c commit 538d103

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/main/java/com/fishercoder/solutions/_134.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ You have a car with an unlimited gas tank and it costs cost[i] of gas to travel
1414
*/
1515
public class _134 {
1616

17-
/**Credit: https://discuss.leetcode.com/topic/5088/my-ac-is-o-1-space-o-n-running-time-solution-does-anybody-have-posted-this-solution*/
17+
public static class Solution1 {
18+
/** Credit: https://discuss.leetcode.com/topic/5088/my-ac-is-o-1-space-o-n-running-time-solution-does-anybody-have-posted-this-solution */
1819
public int canCompleteCircuit(int[] gas, int[] cost) {
19-
int start = gas.length - 1;
20-
int end = 0;
21-
int sum = gas[start] - cost[start];
22-
while (start > end) {
23-
if (sum >= 0) {
24-
sum += gas[end] - cost[end];
25-
end++;
26-
} else {
27-
start--;
28-
sum += gas[start] - cost[start];
29-
}
20+
int start = gas.length - 1;
21+
int end = 0;
22+
int sum = gas[start] - cost[start];
23+
while (start > end) {
24+
if (sum >= 0) {
25+
sum += gas[end] - cost[end];
26+
end++;
27+
} else {
28+
start--;
29+
sum += gas[start] - cost[start];
3030
}
31-
return sum >= 0 ? start : -1;
31+
}
32+
return sum >= 0 ? start : -1;
3233
}
33-
34+
}
3435

3536
}

src/test/java/com/fishercoder/_134Test.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,34 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/27/17.
11-
*/
129
public class _134Test {
13-
private static _134 test;
14-
private static int[] gas;
15-
private static int[] cost;
10+
private static _134.Solution1 solution1;
11+
private static int[] gas;
12+
private static int[] cost;
1613

17-
@BeforeClass
18-
public static void setup() {
19-
test = new _134();
20-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _134.Solution1();
17+
}
2118

22-
@Test
23-
public void test1() {
24-
gas = new int[]{4};
25-
cost = new int[]{5};
26-
assertEquals(-1, test.canCompleteCircuit(gas, cost));
27-
}
19+
@Test
20+
public void test1() {
21+
gas = new int[] {4};
22+
cost = new int[] {5};
23+
assertEquals(-1, solution1.canCompleteCircuit(gas, cost));
24+
}
2825

29-
@Test
30-
public void test2() {
31-
gas = new int[]{5};
32-
cost = new int[]{4};
33-
assertEquals(0, test.canCompleteCircuit(gas, cost));
34-
}
26+
@Test
27+
public void test2() {
28+
gas = new int[] {5};
29+
cost = new int[] {4};
30+
assertEquals(0, solution1.canCompleteCircuit(gas, cost));
31+
}
3532

36-
@Test
37-
public void test3() {
38-
gas = new int[]{2};
39-
cost = new int[]{2};
40-
assertEquals(0, test.canCompleteCircuit(gas, cost));
41-
}
33+
@Test
34+
public void test3() {
35+
gas = new int[] {2};
36+
cost = new int[] {2};
37+
assertEquals(0, solution1.canCompleteCircuit(gas, cost));
38+
}
4239
}

0 commit comments

Comments
 (0)