-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
58 lines (45 loc) · 1.08 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function twoSum(nums: number[], target: number): boolean {
const map = new Map<number, number>();
for (let i = 0; i < nums.length; i += 1) {
const current = nums[i];
const diff = target - current;
if (map.has(diff)) {
return true;
}
map.set(current, i);
}
return false;
}
function part1(input: number[], preambleSize = 25): number | undefined {
for (let i = preambleSize; i < input.length; i += 1) {
const preamble = input.slice(i - preambleSize, i);
const current = input[i];
if (!twoSum(preamble, current)) {
return current;
}
}
return undefined;
}
function part2(input: number[], target: number): number {
let l = 0;
let r = 1;
let s = input[l];
while (true) {
if (s === target) {
const preamble = input.slice(l, r);
const min = Math.min(...preamble);
const max = Math.max(...preamble);
const sum = min + max;
return sum;
}
if (s < target) {
s += input[r];
r += 1;
}
if (s > target) {
s -= input[l];
l += 1;
}
}
}
export { part1, part2 };