package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 170. Two Sum III - Data structure design
 *
 * 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
 */

public class _170 {
  public static class Solution1 {
    private Map<Integer, Integer> map = new HashMap();
    private List<Integer> list = new ArrayList();

    // Add the number to an internal data structure.
    public void add(int number) {
      list.add(number);
      map.put(number, map.getOrDefault(number, 0) + 1);
    }

    // Find if there exists any pair of numbers which sum is equal to the value.
    public boolean find(int value) {
      for (int i = 0; i < list.size(); i++) {
        int val1 = list.get(i);
        int val2 = value - val1;
        if (map.containsKey(val2)) {
          if (val1 == val2) {
            if (map.get(val2) > 1) {
              return true;
            }
          } else {
            return true;
          }
        }
      }
      return false;
    }
  }
}

@@ -133,557 +133,305 @@
 p;%0A%0A
-/**%0A * 170. Two Sum III - Data structure design%0A *%0A * Design and implement a TwoSum class. It should support the following operations: add and find.%0A *%0A * add - Add the number to an internal data structure.%0A * find - Find if there exists any pair of numbers which sum is equal to the value.%0A *%0A * Example 1:%0A * add(1); add(3); add(5);%0A * find(4) -%3E true%0A * find(7) -%3E false%0A *%0A * Example 2:%0A * add(3); add(1); add(2);%0A * find(3) -%3E true%0A * find(6) -%3E false%0A */%0A%0Apublic class _170 %7B%0A  public static class Solution1 %7B%0A    private Map%3CInteger, Integer%3E
+public class _170 %7B%0A    public static class Solution1 %7B%0A        class TwoSum %7B%0A%0A            private Map%3CInteger, Integer%3E map;%0A            private List%3CInteger%3E list;%0A%0A            /**%0A             * Initialize your data structure here.%0A             */%0A            public TwoSum() %7B%0A               
  map
@@ -452,37 +452,27 @@
 ();%0A    
-private List%3CInteger%3E
+           
  list = 
@@ -488,17 +488,40 @@
 List();%0A
-%0A
+            %7D%0A%0A%0A        
     // A
@@ -561,24 +561,32 @@
  structure.%0A
+        
     public v
@@ -603,24 +603,34 @@
 t number) %7B%0A
+          
       list.a
@@ -641,16 +641,26 @@
 umber);%0A
+          
       ma
@@ -711,19 +711,35 @@
 1);%0A    
-%7D%0A%0A
+        %7D%0A%0A        
     // F
@@ -811,16 +811,24 @@
  value.%0A
+        
     publ
@@ -852,24 +852,34 @@
 nt value) %7B%0A
+          
       for (i
@@ -908,24 +908,36 @@
 e(); i++) %7B%0A
+            
         int 
@@ -956,16 +956,28 @@
 get(i);%0A
+            
         
@@ -1001,16 +1001,28 @@
 - val1;%0A
+            
         
@@ -1046,24 +1046,38 @@
 ey(val2)) %7B%0A
+              
           if
@@ -1090,24 +1090,40 @@
  == val2) %7B%0A
+                
             
@@ -1143,24 +1143,42 @@
 al2) %3E 1) %7B%0A
+                  
             
@@ -1196,34 +1196,64 @@
 ue;%0A            
+      
-%7D%0A
+          %7D%0A              
           %7D else
@@ -1255,16 +1255,32 @@
  else %7B%0A
+                
         
@@ -1310,28 +1310,74 @@
     
-%7D%0A        %7D%0A      %7D%0A
+              %7D%0A                    %7D%0A                %7D%0A          
     
@@ -1396,16 +1396,36 @@
 se;%0A    
-%7D%0A
+        %7D%0A        %7D%0A  
   %7D%0A%7D%0A

refactor 170