|
| 1 | +--- |
| 2 | +id: two-sum-III-data-structure-design |
| 3 | +title: Two Sum III - Data Structure Design |
| 4 | +sidebar_label: 0170-Two Sum III - Data Structure Design |
| 5 | +tags: [HashTable, ArrayList, Binary Search] |
| 6 | +description: Design and implement a TwoSum class. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Statement |
| 11 | + |
| 12 | +Design and implement a TwoSum class. It should support the following operations: `add` and `find`. |
| 13 | + |
| 14 | +`add` - Add the number to an internal data structure. |
| 15 | +`find` - Find if there exists any pair of numbers which sum is equal to the value. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +```plaintext |
| 22 | +add(1); add(3); add(5); |
| 23 | +find(4) -> true |
| 24 | +find(7) -> false |
| 25 | +``` |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +```plaintext |
| 30 | +add(3); add(1); add(2); |
| 31 | +find(3) -> true |
| 32 | +find(6) -> false |
| 33 | +``` |
| 34 | + |
| 35 | +**Example 3:** |
| 36 | + |
| 37 | +```plaintext |
| 38 | +Input: numbers = [-1,0], target = -1 |
| 39 | +Output: [1,2] |
| 40 | +Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. |
| 41 | +``` |
| 42 | + |
| 43 | +### Constraints |
| 44 | +$$-10^5 \leq \text{number} \leq 10^5 - 1$$ |
| 45 | + |
| 46 | +$$-2^{31} \leq \text{number} \leq 2^{31} - 1$$ |
| 47 | + |
| 48 | +- At most 104 calls will be made to `add` and `find`. |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +### Approach |
| 53 | + |
| 54 | +One hashmap for unique numbers, and their count (to tackle multiple duplicate numbers issue). |
| 55 | + |
| 56 | + |
| 57 | +#### Algorithm |
| 58 | + |
| 59 | +`HashMap` to hold remainder. |
| 60 | + |
| 61 | +Establish a mapping between each `number` and the number of `occurrences`, and then traverse the HashMap. For each value, first find the difference `t` between this value and the target value, and then you need to look at it in two cases. |
| 62 | + |
| 63 | +- If the current value is `not equal` to the difference t, then return True as long as there is a difference t in the HashMap, or when the difference t is equal to the current value, |
| 64 | + |
| 65 | +- If the number of mapping times of the HashMap is `greater than 1`, it means that there is another number in the HashMap that is `equal` to the current value, and the addition of the two is the target value |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +#### Implementation |
| 70 | + |
| 71 | +```Java |
| 72 | +class TwoSum { |
| 73 | + Map<Integer,Boolean> map; |
| 74 | + List<Integer> list; |
| 75 | + int low = Integer.MAX_VALUE; |
| 76 | + int high = Integer.MIN_VALUE; |
| 77 | + /** Initialize your data structure here. */ |
| 78 | + public TwoSum() { |
| 79 | + map = new HashMap<>(); |
| 80 | + list = new LinkedList<>(); |
| 81 | + } |
| 82 | + |
| 83 | + /** Add the number to an internal data structure..*/ |
| 84 | + public void add(int number) { |
| 85 | + if(map.containsKey(number)){ |
| 86 | + map.put(number,true); |
| 87 | + }else{ |
| 88 | + map.put(number,false); |
| 89 | + list.add(number); |
| 90 | + low = Math.min(low,number); |
| 91 | + high = Math.max(high,number); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** Find if there exists any pair of numbers which sum is equal |
| 96 | + * to the value. */ |
| 97 | + public boolean find(int value) { |
| 98 | + if(value < 2* low || value > 2*high) return false; |
| 99 | + for(int num : list){ |
| 100 | + int target = value - num; |
| 101 | + if(map.containsKey(target)){ |
| 102 | + if(num != target) return true; |
| 103 | + else if(map.get(target)) return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +### Complexity Analysis |
| 113 | + |
| 114 | +- **Time complexity**: |
| 115 | + * $O(N)$ - `find` |
| 116 | + * $O(1)$ - `add` |
| 117 | + |
| 118 | +- **Space complexity**: $O(N)$ |
| 119 | + * The HashMap takes the space of $O(N)$ |
0 commit comments