In [1]:
import numpy as np
from typing import List
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<string> strs) {
// ... your code
return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
//... your code
return strs;
}
So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector<string> 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.
In [7]:
# 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))
Out[7]:
In [44]:
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
In [47]:
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"]]))
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] < A[1] < A[2] < ... < 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].
In [64]:
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
In [65]:
print(Solution().minSwap([2,3,2,5,6], [0,1,4,4,5]))
In [ ]: