Skip to content

Commit 4173cdc

Browse files
committed
solve 0415
1 parent c37f33e commit 4173cdc

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
2+
import { addStrings } from "./0415_add_strings.ts";
3+
4+
Deno.test(function test1() {
5+
const num1 = "1";
6+
const num2 = "1";
7+
assertEquals(addStrings(num1, num2), "2");
8+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
3+
*
4+
* Note:
5+
* - The length of both num1 and num2 is < 5100.
6+
* - Both num1 and num2 contains only digits 0-9.
7+
* - Both num1 and num2 does not contain any leading zero.
8+
* - You must not use any built-in BigInteger library or convert the inputs to integer directly.
9+
*/
10+
11+
const addStrings = (num1: string, num2: string): string => {
12+
let num1Cursor = num1.length - 1;
13+
let num2Cursor = num2.length - 1;
14+
let result = "";
15+
16+
let carry = 0;
17+
18+
while (num1Cursor >= 0 || num2Cursor >= 0) {
19+
const digit1 = parseInt(num1[num1Cursor]) || 0;
20+
const digit2 = parseInt(num2[num2Cursor]) || 0;
21+
const sum = digit1 + digit2 + carry;
22+
23+
carry = Math.floor(sum / 10);
24+
25+
result = (sum % 10).toString() + result;
26+
num1Cursor--;
27+
num2Cursor--;
28+
}
29+
30+
if (carry > 0) {
31+
result = `${carry}${result}`;
32+
}
33+
34+
return result;
35+
};
36+
37+
export { addStrings };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
2+
import { kClosest } from "./0973_k_closest_points_to_origin.ts";
3+
4+
Deno.test(function test1() {
5+
const points = [
6+
[1, 3],
7+
[-2, 2],
8+
];
9+
const K = 1;
10+
const output = [[-2, 2]];
11+
12+
assertEquals(kClosest(points, K), output);
13+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* We have a list of points on the plane.
3+
* Find the K closest points to the origin (0, 0).
4+
*
5+
* (Here, the distance between two points on a plane is the Euclidean distance.)
6+
*
7+
* You may return the answer in any order.
8+
*
9+
* The answer is guaranteed to be unique (except for the order that it is in.)
10+
*/
11+
12+
type Args = {
13+
x1: number;
14+
y1: number;
15+
x2: number;
16+
y2: number;
17+
};
18+
19+
const distanceBetweenPoints = ({ x1, y1, x2, y2 }: Args) => {
20+
// d=√((x_2-x_1)²+(y_2-y_1)²)
21+
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
22+
};
23+
24+
const kClosest = (points: number[][], K: number): number[][] => {
25+
const distances = [];
26+
27+
for (const currentPoints of points) {
28+
const [x, y] = currentPoints;
29+
distances.push({
30+
points: currentPoints,
31+
distance: distanceBetweenPoints({ x1: 0, y1: 0, x2: x, y2: y }),
32+
});
33+
}
34+
35+
distances.sort((a, b) => a.distance - b.distance);
36+
37+
return distances.slice(0, K).map((distances) => distances.points);
38+
};
39+
40+
export { kClosest };

0 commit comments

Comments
 (0)