-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-two-sum.ts
62 lines (54 loc) · 1.82 KB
/
01-two-sum.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
59
60
61
62
/**
Given an array of integers number and an integer target, return indices of the two numbers such
that they add up to target. You may assume that each input would have exactly one solution, and
you may not use the same element twice. You can return the answer in any order.
Example 1:
Input: number = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because number[0] + number[1] == 9, we return [0, 1].
Example 2:
Input: number = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: number = [3,3], target = 6
Output: [0,1]
*/
function twoSumBruteForce(numArray: number[], target: number) {
for (let i = 0; i < numArray.length; i++) {
for (let j = i + 1; j < numArray.length; j++) {
if (numArray[i] + numArray[j] === target) {
return [i, j];
}
}
}
return [];
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('Two sum brute force test cases', () => {
expect(twoSumBruteForce([2, 7, 11, 15], 9)).to.eql([0, 1]);
expect(twoSumBruteForce([3, 2, 4], 6)).to.eql([1, 2]);
expect(twoSumBruteForce([3, 3], 6)).to.eql([0, 1]);
expect(twoSumBruteForce([3, 3, 3, 4], 60)).to.eql([]);
});
}
function twoSumUsingMap(numArray: number[], target: number) {
const map = new Map<number, number>();
for (let i = 0; i < numArray.length; i++) {
const subtractedValue = target - numArray[i];
if (map.has(subtractedValue)) {
return [map.get(subtractedValue), i];
}
map.set(numArray[i], i);
}
return [];
}
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
it('Two sum using map test cases', () => {
expect(twoSumUsingMap([2, 7, 11, 15], 9)).to.eql([0, 1]);
expect(twoSumUsingMap([3, 2, 4], 6)).to.eql([1, 2]);
expect(twoSumUsingMap([3, 3], 6)).to.eql([0, 1]);
expect(twoSumUsingMap([3, 3, 3, 4], 60)).to.eql([]);
});
}