-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path170. Two Sum III - Data structure design.js
116 lines (105 loc) · 2.94 KB
/
170. Two Sum III - Data structure design.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
Design and implement a TwoSum class. It should support the following operations: add and find.
add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.
Example 1:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
Example 2:
add(3); add(1); add(2);
find(3) -> true
find(6) -> false
*/
/**
* Algorithm1: Frequent add, less find (large dataset)
* In constructor:
* 1. Declare map(number: count) to store number and how many times it is added to
* In add(number): O(1)
* 1. If number in map: map[number] += 1, else: map[number] = 1
* In find(value): O(n)
* 1. Use map.keys to iterate all the numbers in map as a
* 2. let b = value - a
* 3. If a == b: check if there is more than more count in map[a] return true
* 4. If b in map: return true
* 5. return false
*
* Algorithm2: Frequent find, less add
* In constructor:
* 1. Declare nums:set(number) to store numbers added in
* 2. Declare sum:set(number) to store valid two sum
* In add(number): O(n^2):
* 1. If number in nums: sum.add(num*2)
* 2. else: iterate num in nums and sum.add(num+number)
* In find(value): O(1):
* 1. return value in sum
*/
// Algorithm1
class TwoSum {
constructor() {
this.map = {};
}
/**
* Add the number to an internal data structure..
* @param {number} number
* @return {void}
*/
add(number) {
// Construct map(something, count) SOP!
this.map[number] = this.map[number] || 0;
this.map[number] += 1;
}
/**
* Find if there exists any pair of numbers which sum is equal to the value.
* @param {number} value
* @return {boolean}
*/
find(value) {
for (let key in this.map) {
// Remember to parse key into number!
/**
* If not Number(key)
* typeof key === 'string'
* diff = 0 - '0' = 0
*/
key = Number(key);
let diff = value - key;
if (key === diff && this.map[key] > 1) return true;
if (key !== diff && this.map.hasOwnProperty(diff)) return true;
}
return false
}
}
let twoSum = new TwoSum();
twoSum.add(0);
console.log(twoSum.find(0)); // false
// Algorithm2
class TwoSum2 {
constructor() {
this.nums = new Set();
this.sum = new Set();
}
/**
* Add the number to an internal data structure..
* @param {number} number
* @return {void}
*/
add(number) {
if (this.nums.has(number)) {
this.sum.add(number*2);
} else {
for (let num of this.nums) {
this.sum.add(number+num);
}
this.nums.add(number);
}
}
/**
* Find if there exists any pair of numbers which sum is equal to the value.
* @param {number} value
* @return {boolean}
*/
find(value) {
return this.sum.has(value);
}
}