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

Commit 53268c0

Browse files
add 1184
1 parent f0d37db commit 53268c0

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _If you like this project, please leave me a star._ ★
1919
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | || [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
2020
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | | | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo)|Easy||
2121
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | | | |Easy||
22+
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1184.java) | | | |Easy||
2223
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | | | |Easy||
2324
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
2425
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1184. Distance Between Bus Stops
5+
*
6+
* A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.
7+
* The bus goes along both directions i.e. clockwise and counterclockwise.
8+
* Return the shortest distance between the given start and destination stops.
9+
*
10+
* Example 1:
11+
* Input: distance = [1,2,3,4], start = 0, destination = 1
12+
* Output: 1
13+
* Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
14+
*
15+
* Example 2:
16+
* Input: distance = [1,2,3,4], start = 0, destination = 2
17+
* Output: 3
18+
* Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
19+
*
20+
* Example 3:
21+
* Input: distance = [1,2,3,4], start = 0, destination = 3
22+
* Output: 4
23+
* Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
24+
*
25+
* Constraints:
26+
* 1 <= n <= 10^4
27+
* distance.length == n
28+
* 0 <= start, destination < n
29+
* 0 <= distance[i] <= 10^4
30+
* */
31+
public class _1184 {
32+
public static class Solution1 {
33+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
34+
if (start > destination) {
35+
int tmp = start;
36+
start = destination;
37+
destination = tmp;
38+
}
39+
int clockwise = 0;
40+
for (int i = start; i < destination; i++) {
41+
clockwise += distance[i];
42+
}
43+
int counterClockwise = 0;
44+
for (int i = destination; i < distance.length; i++) {
45+
counterClockwise += distance[i];
46+
}
47+
for (int i = 0; i < start; i++) {
48+
counterClockwise += distance[i];
49+
}
50+
51+
return Math.min(clockwise, counterClockwise);
52+
}
53+
}
54+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1184;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1184Test {
10+
private static _1184.Solution1 solution1;
11+
private static int[] distance;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1184.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
distance = new int[]{1, 2, 3, 4};
21+
assertEquals(1, solution1.distanceBetweenBusStops(distance, 0, 1));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
distance = new int[]{1, 2, 3, 4};
27+
assertEquals(4, solution1.distanceBetweenBusStops(distance, 0, 3));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
distance = new int[]{1, 2, 3, 4};
33+
assertEquals(3, solution1.distanceBetweenBusStops(distance, 0, 2));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
distance = new int[]{7,10,1,12,11,14,5,0};
39+
assertEquals(17, solution1.distanceBetweenBusStops(distance, 7, 2));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)