Skip to content

Commit 999e4e8

Browse files
committed
https://leetcode.cn/problems/distance-between-bus-stops/
1 parent d666024 commit 999e4e8

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ https://leetcode.cn/problems/o8SXZn
886886

887887
https://leetcode.cn/problems/insert-delete-getrandom-o1-duplicates-allowed/
888888

889+
https://leetcode.cn/problems/distance-between-bus-stops/
890+
889891
https://leetcode.cn/problems/find-median-from-data-stream/
890892

891893
https://leetcode.cn/problems/replace-words/

deps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export {
2222
BinarySearchTree,
2323
BinarySearchTreeNode,
2424
} from "https://cdn.skypack.dev/@datastructures-js/[email protected]/index.js?dts";
25+
export { default as groupBy } from "https://cdn.skypack.dev/[email protected]/groupBy?dts";
26+
export { default as sum } from "https://cdn.skypack.dev/[email protected]/sum?dts";

distance-between-bus-stops/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function distanceBetweenBusStops(
2+
distance: number[],
3+
start: number,
4+
destination: number,
5+
): number {
6+
[start, destination] = [
7+
Math.min(start, destination),
8+
Math.max(start, destination),
9+
];
10+
11+
return Math.min(
12+
// deno-lint-ignore ban-ts-comment
13+
//@ts-ignore
14+
...Object.values(
15+
groupBy(
16+
Array.from(distance.entries()),
17+
([index]) => start <= index && index < destination,
18+
),
19+
// deno-lint-ignore ban-ts-comment
20+
//@ts-ignore
21+
).map((value) => sum(value.map((a) => a[1]))),
22+
);
23+
}
24+
export default distanceBetweenBusStops;
25+
import { groupBy, sum } from "../deps.ts";

distance-between-bus-stops/test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assertEquals } from "../deps.ts";
2+
import distanceBetweenBusStops from "./index.ts";
3+
4+
Deno.test("distance-between-bus-stops", () => {
5+
const distance = [1, 2, 3, 4];
6+
const start = 0;
7+
const destination = 3;
8+
const expected = 4;
9+
const actual = distanceBetweenBusStops(distance, start, destination);
10+
assertEquals(actual, expected);
11+
12+
const distance2 = [1, 2, 3, 4];
13+
const start2 = 0;
14+
const destination2 = 2;
15+
const expected2 = 3;
16+
const actual2 = distanceBetweenBusStops(distance2, start2, destination2);
17+
assertEquals(actual2, expected2);
18+
19+
const distance3 = [1, 2, 3, 4];
20+
const start3 = 0;
21+
const destination3 = 1;
22+
const expected3 = 1;
23+
24+
const actual3 = distanceBetweenBusStops(distance3, start3, destination3);
25+
assertEquals(actual3, expected3);
26+
assertEquals(
27+
distanceBetweenBusStops([7, 10, 1, 12, 11, 14, 5, 0], 7, 2),
28+
17,
29+
);
30+
});

0 commit comments

Comments
 (0)