-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path170__easy__two-sum-iii-data-structure-design.js
74 lines (63 loc) · 1.57 KB
/
170__easy__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
/*
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
*/
/**
* use hashmap for add, use loop for find
* O(1) for add
* O(n) for find
* O(n) space
*
* 6m
*
* I don't think you should store all combinations here
* the space you need is n^2 and add become O(n) even though your find become O(1)
* this is still worse than the 1st solution, ex:
* O(n) add
* O(1) find,
* O(n^2) space
*
*/
/**
* Initialize your data structure here.
*/
var TwoSum = function() {
this.hm = new Map();
};
/**
* Add the number to an internal data structure..
* @param {number} number
* @return {void}
*/
TwoSum.prototype.add = function(number) {
this.hm.set(number, (this.hm.get(number) || 0) + 1);
};
/**
* Find if there exists any pair of numbers which sum is equal to the value.
* @param {number} value
* @return {boolean}
*/
TwoSum.prototype.find = function(value) {
for (let item of this.hm) {
let target = value - item[0];
if (this.hm.has(target)) {
if (target !== item[0] || this.hm.get(target) > 1) return true;
}
}
return false;
};
/**
* Your TwoSum object will be instantiated and called as such:
* var obj = Object.create(TwoSum).createNew()
* obj.add(number)
* var param_2 = obj.find(value)
*/