-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo-sum.js
82 lines (70 loc) · 2.48 KB
/
two-sum.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*
* Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
*
*
*/
// *-+*- *-+*- *-+*- *-+*- *-+*- SOLUTION 1
// var twoSum = function(nums, target) {
// // console.log("nums", nums);
// console.log("target", target);
//
// // initialize indexes arr
// const indexes = [];
// // loop over nums
// console.log(`idx | val`);
// for(let i = 0; i < nums.length; i++) {
// console.log(`${i} --- ${nums[i]}`);
// // check if nums[i] is less than target
// // check differece between target and num[i] exists as value in nums
// const secondIndex = nums.indexOf(target - nums[i]);
// console.log("secondIndex", secondIndex);
//
// // if num is found return arr of indexes
// if(secondIndex > -1 && secondIndex !== i) {
// const indexArr = [i, secondIndex];
// return indexArr
// };
// console.log("second index -- " , secondIndex);
// };
// };
// *-+*- *-+*- *-+*- *-+*- *-+*- SOLUTION 2
var twoSum = function(nums, target) {
// create obj to store all values of nums
const numsMap = {};
// loop over nums to fill up map
for(let i = 0; i < nums.length; i++) {
const key = nums[i];
const value = i;
if(numsMap[key]) {
numsMap[key].push(value);
} else {
numsMap[`${key}`] = [value];
}
};
// return the indexes of the matching value
for(let num in numsMap) {
// get key to first index and difference needed to meet target
const index1 = parseInt(num);
const difference = target - index1;
// check if difference exists on map
if(numsMap[`${difference}`]) {
// if difference is in map then get index values from map and return array
const indexes = [numsMap[`${index1}`][0], numsMap[`${difference}`][numsMap[`${difference}`].length - 1]]
return indexes;
}
};
};
// -+^+- -+^+- -+^+- -+^+- -+^+- TESTS
const inputs1 = {nums: [1, 3, 4, 2], target:3};
const inputs2 = {nums: [1, 2, 3, 4], target:4};
const inputs3 = {nums: [1, 2, 3, 4], target:6};
const inputs4 = {nums: [-1,-2,-3,-4,-5], target:-8};
const inputs5 = {nums:[3,2,4], target:6}; // [1,2]
//console.log(twoSum(inputs1.nums, inputs1.target)); // [0,3]
//console.log(twoSum(inputs2.nums, inputs2.target)); // [0,2]
//console.log(twoSum(inputs3.nums, inputs3.target)); // [1,3]
console.log(twoSum(inputs5.nums, inputs5.target)); // [2,5]