# NOTE: We assume <EOS> is a special token that shows that the string has ended
#       and the next string has started.
class Codec:

    def encode(self, strs):
        """Encodes a list of strings to a single string.
        
        :type strs: List[str]
        :rtype: str
        """
        if strs == []:
            return None
        
        return '<EOS>'.join(strs)

    def decode(self, s):
        """Decodes a single string to a list of strings.
        
        :type s: str
        :rtype: List[str]
        """
        if s == None:
            return []
        elif s == '':
            return [""]
        
        return s.split("<EOS>")

# Your Codec object will be instantiated and called as such:
strs = ["Hello", "World"]
codec = Codec()
codec.decode(codec.encode(strs))

"""
Explanation: P1. Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
Machine 1 (sender) has the function:
string encode(vector&lt;string&gt; strs) {
  // ... your code
  return encoded_string;
}
Machine 2 (receiver) has the function:
vector&lt;string&gt; decode(string s) {
  //... your code
  return strs;
}
So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector&lt;string&gt; strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.
Implement the encode and decode methods.
Note:
The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters.
Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm.
End of explanation
"""


def find_largest_square(matrix) -> int:
    if matrix == []:
        return 0
    
    size = (len(matrix), len(matrix[0]))
    max_sq_size = size[0] if size[0] < size[1] else size[1]
    
    def get_square_size(pos):
        sq_size = 1
        while sq_size <= max_sq_size and (pos[0] + sq_size) <= size[0] and (pos[1] + sq_size) <= size[1]:
            for i in range(pos[0], pos[0] + sq_size):
                for j in range(pos[1], pos[1] + sq_size):
                    if matrix[i][j] == "0":
                        return (sq_size - 1) * (sq_size - 1)
            
            sq_size += 1
        
        return (sq_size - 1) * (sq_size - 1)
    
    max_size = 0
    for i in range(0, size[0]):
        for j in range(0, size[1]):
            if matrix[i][j] == "1":
                curr_size = get_square_size((i, j))
                if  curr_size > max_size:
                    max_size = curr_size 
    
    return max_size

print(find_largest_square([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]))
print(find_largest_square([]))
print(find_largest_square([["1","0"],["1","0"]]))
print(find_largest_square([["1"]]))

"""
Explanation: P2. Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
```
Example:
Input: 
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
```
End of explanation
"""


class Solution(object):
    def minSwap(self, A, B):
        """
        :type A: List[int]
        :type B: List[int]
        :rtype: int
        """
        size = len(A)
        i = 0
        num_swaps = 0
        print(A, B)
        while i < size - 1:
            if A[i] >= A[i + 1] or B[i] >= B[i + 1]:
                A[i], B[i] = B[i], A[i]
                print(A, B, i)
                num_swaps += 1
                if i > 0:
                    i -= 1
            else:
                i += 1
        
        return num_swaps

print(Solution().minSwap([2,3,2,5,6], [0,1,4,4,5]))

"""
Explanation: P3.
We have two integer sequences A and B of the same non-zero length.
We are allowed to swap elements A[i] and B[i].  Note that both elements are in the same index position in their respective sequences.
At the end of some number of swaps, A and B are both strictly increasing.  (A sequence is strictly increasing if and only if A[0] &lt; A[1] &lt; A[2] &lt; ... &lt; A[A.length - 1].)
Given A and B, return the minimum number of swaps to make both sequences strictly increasing.  It is guaranteed that the given input always makes it possible.
Example:
Input: A = [1,3,5,4], B = [1,2,3,7]
Output: 1
Explanation: 
Swap A[3] and B[3].  Then the sequences are:
A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
which are both strictly increasing.
Note:
A, B are arrays with the same length, and that length will be in the range [1, 1000].
A[i], B[i] are integer values in the range [0, 2000].
End of explanation
"""


