package com.fishercoder.solutions;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**Implement an iterator to flatten a 2d vector.

 For example,
 Given 2d vector =

 [
 [1,2],
 [3],
 [4,5,6]
 ]
 By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].

 Hint:

 How many variables do you need to keep track?
 Two variables is all you need. Try with x and y.
 Beware of empty rows. It could be the first few rows.
 To write correct code, think about the invariant to maintain. What is it?
 The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
 Not sure? Think about how you would implement hasNext(). Which is more complex?
 Common logic in two different places should be refactored into a common method.

 Follow up:
 As an added challenge, try to code it using only iterators in C++ or iterators in Java.*/

public class _251 {

    class Vector2D implements Iterator<Integer> {
        private Queue<Integer> cache;
        private List<List<Integer>> vec2d;

        public Vector2D(List<List<Integer>> vec2d) {
            this.vec2d = vec2d;
            this.cache = new LinkedList<Integer>();
            if (vec2d != null && vec2d.size() > 0) {
                for (List<Integer> list : vec2d) {
                    for (int i : list) {
                        cache.offer(i);
                    }
                }
            }
        }

        @Override
        public Integer next() {
            return cache.poll();
        }

        @Override
        public boolean hasNext() {
            return !cache.isEmpty();
        }
    }

}
@@ -137,922 +137,69 @@
 e;%0A%0A
-/**Implement an iterator to flatten a 2d vector.%0A%0A For example,%0A Given 2d vector =%0A%0A %5B%0A %5B1,2%5D,%0A %5B3%5D,%0A %5B4,5,6%5D%0A %5D%0A By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: %5B1,2,3,4,5,6%5D.%0A%0A Hint:%0A%0A How many variables do you need to keep track?%0A Two variables is all you need. Try with x and y.%0A Beware of empty rows. It could be the first few rows.%0A To write correct code, think about the invariant to maintain. What is it?%0A The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?%0A Not sure? Think about how you would implement hasNext(). Which is more complex?%0A Common logic in two different places should be refactored into a common method.%0A%0A Follow up:%0A As an added challenge, try to code it using only iterators in C++ or iterators in Java.*/%0A%0Apublic class _251 %7B%0A%0A
+public class _251 %7B%0A%0A    public static class Solution1 %7B%0A    
     
@@ -244,16 +244,20 @@
 eger%3E %7B%0A
+    
         
@@ -286,32 +286,36 @@
  cache;%0A        
+    
 private List%3CLis
@@ -334,16 +334,20 @@
 vec2d;%0A%0A
+    
         
@@ -399,24 +399,28 @@
             
+    
 this.vec2d =
@@ -435,24 +435,28 @@
             
+   
+ 
 this.cache =
@@ -479,24 +479,28 @@
 Integer%3E();%0A
+    
             
@@ -552,24 +552,28 @@
             
+   
+ 
 for (List%3CIn
@@ -611,24 +611,28 @@
             
+    
 for (int i :
@@ -664,16 +664,20 @@
         
+  
+  
 cache.of
@@ -696,34 +696,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -720,34 +720,40 @@
                 
-%7D%0A
+  %7D%0A    
             %7D%0A  
@@ -750,35 +750,43 @@
       %7D%0A        
+ 
-%7D%0A%0A
+   %7D%0A%0A    
         @Overrid
@@ -791,24 +791,28 @@
 ide%0A        
+ 
+   
 public Integ
@@ -815,32 +815,36 @@
 nteger next() %7B%0A
+    
             retu
@@ -860,35 +860,43 @@
 poll();%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         @Overrid
@@ -901,24 +901,28 @@
 ide%0A        
+    
 public boole
@@ -944,24 +944,28 @@
             
+   
+ 
 return !cach
@@ -973,24 +973,38 @@
 .isEmpty();%0A
+            %7D%0A
         %7D%0A  
@@ -1007,10 +1007,9 @@
 %7D%0A    %7D%0A
-%0A
 %7D

refactor 251