DP Practice - 2
DP Practice - 2
Example
{
"s": [10, -3, 7, 2, 1, 3]
}
Output:
[1, 1, 0, 0, 0, 1]
There are multiple partitionings, where s1 sums up to 10 and s2 sums up to 10; they are all correct answers:
s1 = [ 10 , -3 , 3 ] and s2 = [ 7 , 2 , 1 ] (Sample output)
s1 = [ 7 , 2 , 1 ] and s2 = [ 10 , -3 , 3 ]
s1 = [10] and s2 = [-3, 3, 7, 2, 1]
s1 = [-3, 3, 7, 2, 1] and s2 = [10]
s1 = [10, -3, 2, 1] and s2 = [7, 3]
s1 = [7, 3] and s2 = [10, -3, 2, 1].
Notes
Constraints:
def equal_subset_sum_partition(s):
"""
Args:
s(list_int32)
Returns:
list_bool
"""
# Write your code here.
return []
Problem
Levenshtein Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
(each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
Insert a character
Delete a character
Replace a character
The minimum number of steps required to convert word1 to word2 with the given set of allowed operations is
called edit distance. e.g. Minimum edit distance between the words 'kitten' and 'sitting', is 3.
Example One
{
"word1": "cat",
"word2": "bat"
}
Output:
Example Two
{
"word1": "qwe",
"word2": "q"
}
Output:
Notes
Constraints:
Example
{
"dictionary": ["kick", "start", "kickstart", "is", "awe", "some", "awesome"],
"txt": "kickstartisawesome"
}
Output:
4
Here are all four ways to break down the string into the dictionary words:
Notes
Constraints:
Problem
Largest Square Submatrix With All 1s
Given a two-dimensional binary matrix of size n * m, find the largest square submatrix with all 1s.
Example
{
"n": 3,
"m": 3,
"mat": [
[1, 0, 0],
[0, 1, 1],
[0, 1, 1]
]
}
Output:
2
2x2 submatrix at right-bottom has all 1s. That’s the largest one. Length of its side is 2.
Notes
Output is an integer, the length of the side of the largest square submatrix with all 1s.
Constraints:
Problem
Word Wrap
Given a sequence of words (strings), and a limit on the number of characters that can be put in one line (line
width), put line breaks in the given sequence such that the lines are printed neatly.
The word processors like MS Word do the task of placing line breaks. The idea is to have balanced lines. In
other words, not have a few lines with lots of extra spaces and some lines with a small amount of extra
spaces.
The extra spaces means spaces put at the end of every line.
Put line breaks such that the following total cost is minimized:
Example One
{
"words": ["abcdefghijkl", "abcdefg", "abcdefgh", "abcdefghijklmnopqrstuv"],
"limit": 23
}
Output:
1674
The following arrangement of words in lines will have least cost:
Line1: "abcdefghijkl"
Line2: "abcdefg abcdefgh"
Line3: "abcdefghijklmnopqrstuv"
Note that we need to ignore the extra white spaces at the end of the last line. So, in the last line there will be
0 extra white spaces.
Cost for this configuration: (23 - 12)3 + (23 - (7 + 1 + 8))3 + (0)3 = 1674
Example Two
{
"words": ["omg", "very", "are", "extreme"],
"limit": 10
}
Output:
351
The following arrangement of words in lines will have least cost:
Note that we need to ignore the extra white spaces at the end of the last line. So, in the last line there will be
0 extra white spaces.
Cost for this configuration: (10 - (3 + 1 + 4))3 + (10 - 3)3 + (0)3 = 351
Notes
Each word belongs to a single line and no word can be partially in one line and another part in a
different line.
Assume that the length of each word is smaller than or equal to the line width.
Extra spaces means spaces put at the end of every line means white spaces between two words need
to be ignored.
Two words in a line will have exactly one space in between.
Ignore extra white spaces at the end of the last line.
Note that the total cost function is not the sum of extra spaces, but sum of cubes of extra spaces.
Constraints:
Word Wrap
Problem
Strings Interleave
You are given three strings: a, b and i, write a function that checks whether i is an interleaving of a and b.
String i is said to be interleaving string a and b, if:
Example One
{
"a": "123",
"b": "456",
"i": "123456"
}
Output:
true
Example Two
{
"a": "AAB",
"b": "AAC",
"i": "AAAABC"
}
Output:
true
Notes
Constraints:
A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements
without changing the order of the remaining elements.
Example
{
"a": "ABCDE",
"b": "AECBD"
}
Output:
"ABD"
Subsequence "ABD" can be derived from the first string by deleting characters "C" and "E". From the second
string it can be derived by deleting "E" and "C". No common subsequence longer than three characters
exists in the two given strings. "ACD" is another common subsequence of length three; it is also a correct
answer.
Notes
Constraints:
Problem
We have many options to multiply a chain of matrices because matrix multiplication is associative. In other
words, no matter how we parenthesize the product, the result will be the same. For example, if we had four
matrices A, B, C, and D, we would have:
(ABC)D = (AB)(CD) = A(BCD) = ....
However, the order in which we parenthesize the product affects the number of simple arithmetic operations
needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5
matrix, and C is a 5 × 60 matrix. Then,
(AB)C = (10 × 30 × 5) + (10 × 5 × 60) = 1500 + 3000 = 4500 operations
A(BC) = (30 × 5 × 60) + (10 × 30 × 60) = 9000 + 18000 = 27000 operations.
Clearly, the first parenthesization requires less number of operations.
Given an array matrix_sizes, which represents the chain of matrices such that the i-th
matrix matrix_sizes[i] is of dimension matrix_sizes[i - 1] x matrix_sizes[i] , we need to write a function
that should return the minimum number of multiplications needed to multiply the chain. Length of the chain
of matrices is n and thus the size of matrix_sizes is n + 1.
Example
{
"matrix_sizes": [10, 30, 5, 60]
}
Output:
4500
Notes
For any matrix, either both the dimensions will be zero, or both the dimensions will be non zero.
Constraints:
def minimum_multiplication_cost(matrix_sizes):
"""
Args:
matrix_sizes(list_int32)
Returns:
int32
"""
# Write your code here.
return 0