forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJeehay28.ts
More file actions
42 lines (32 loc) · 1.36 KB
/
Jeehay28.ts
File metadata and controls
42 lines (32 loc) · 1.36 KB
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
// Approach 2: HashMap (Using Map in TypeScript)
// ⏳ Time Complexity: O(n) ✅ (Efficient for large inputs)
// 💾 Space Complexity: O(n) ❌ (Extra memory used for Map)
function twoSum(nums: number[], target: number): number[] {
const indices = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const temp = target - nums[i];
if (indices.has(temp)) {
// Ensure correct order
return [indices.get(temp)!, i];
// The exclamation mark (!) in indices.get(temp)! is a non-null assertion operator in TypeScript.
}
// If you return [i, indices.get(temp)!],
// you would be returning the current index first,
// which is incorrect because the problem statement usually expects indices in the order they were found (earlier index first).
indices.set(nums[i], i);
}
return [];
};
// Approach 1: Brute Force (O(n^2))
// ⏳ Time Complexity: O(n^2) ❌ (Inefficient for large inputs)
// 💾 Space Complexity: O(1) ✅ (Great, no extra memory used!)
// function twoSum(nums: number[], target: number): number[] {
// for (let i = 0; i < nums.length; i++) {
// for (let j = i + 1; j < nums.length; j++) {
// if (nums[i] + nums[j] === target) {
// return [i, j]
// }
// }
// }
// return [];
// };