File tree Expand file tree Collapse file tree 2 files changed +40
-42
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +40
-42
lines changed Original file line number Diff line number Diff line change @@ -14,22 +14,23 @@ You have a car with an unlimited gas tank and it costs cost[i] of gas to travel
14
14
*/
15
15
public class _134 {
16
16
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 */
18
19
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 ];
30
30
}
31
- return sum >= 0 ? start : -1 ;
31
+ }
32
+ return sum >= 0 ? start : -1 ;
32
33
}
33
-
34
+ }
34
35
35
36
}
Original file line number Diff line number Diff line change 6
6
7
7
import static org .junit .Assert .assertEquals ;
8
8
9
- /**
10
- * Created by fishercoder on 5/27/17.
11
- */
12
9
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 ;
16
13
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
+ }
21
18
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
+ }
28
25
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
+ }
35
32
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
+ }
42
39
}
You can’t perform that action at this time.
0 commit comments