-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-two-sum-3.js
34 lines (29 loc) · 911 Bytes
/
1-two-sum-3.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
/**
Given an array of integers nums 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.
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const sortedNums = nums
.map((num, index) => ({ num, index }))
.sort((a, b) => a.num - b.num);
let left = 0;
let right = sortedNums.length - 1;
while (left < right) {
const sum = sortedNums[left].num + sortedNums[right].num;
if (sum === target) {
return [sortedNums[left].index, sortedNums[right].index];
} else if (sum < target) {
left++;
} else {
right--;
}
}
};
// Monday, September 2, 2024 at 7:12 AM CST
// B Salinas + Cursor + Claude 3.5 Sonnet