0% found this document useful (0 votes)
54 views9 pages

DP Practice - 2

The problem is to find the most efficient way to multiply a sequence of matrices by determining the optimal parenthesization. Dynamic programming is used to calculate the minimum number of operations needed by trying all possible parenthesizations and choosing the one with the fewest operations. The optimal parenthesization and number of operations are returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views9 pages

DP Practice - 2

The problem is to find the most efficient way to multiply a sequence of matrices by determining the optimal parenthesization. Dynamic programming is used to calculate the minimum number of operations needed by trying all possible parenthesizations and choosing the one with the fewest operations. The optimal parenthesization and number of operations are returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Problem

Equal Subset Partition


Given an array s of n integers, partition it into two non-empty subsets, s1 and s2, such that the sum of all
elements in s1 is equal to the sum of all elements in s2. Return a boolean array of size n, where i-th element
is 1 if i-th element of s belongs to s1 and 0 if it belongs to s2.

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

 Any valid answer will be accepted.


 If such partitioning is not possible, return an empty array.

Constraints:

 1 <= n <= 100


 -100 <= elements in s <= 100

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.

 kitten → sitten (substitution of "s" for "k")


 sitten → sittin (substitution of "i" for "e")
 sittin → sitting (insertion of "g" at the end)

Read more about edit distance here.

Example One
{
"word1": "cat",
"word2": "bat"
}
Output:

Example Two
{
"word1": "qwe",
"word2": "q"
}
Output:

Notes

Constraints:

 1 <= length of the strings word1 and word2 <= 105


 word1 and word2 contains lower case alphabets from 'a' to 'z'.

def levenshtein_distance(word1, word2):


"""
Args:
word1(str)
word2(str)
Returns:
int32
"""
# Write your code here.
return 0
Problem

Word Break Count


Given a dictionary of words and a string txt, find the number of ways the string can be broken down into
the dictionary words. Return the answer modulo 10 9 + 7.

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:

1. kick start is awe some


2. kick start is awesome
3. kickstart is awe some
4. kickstart is awesome

4 % 1000000007 = 4 so the correct output is 4.

Notes

Constraints:

 1 <= number of words in the dictionary <= 2 * 10 5


 1 <= length of any dictionary word <= 102
 1 <= length of the string txt <= 2 * 103
 Dictionary words and the string txt all consist of lowercase latin characters only (no whitespace, in
particular).

def word_break_count(dictionary, txt):


"""
Args:
dictionary(list_str)
txt(str)
Returns:
int32
"""
# Write your code here.
return 0

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:

 1 <= n, m <= 1000

def largest_sub_square_matrix(n, m, mat):


"""
Args:
n(int32)
m(int32)
mat(list_list_int32)
Returns:
int32
"""
# Write your code here.
return 0

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:

1. Cost of a line = (number of extra spaces in the line) 3


2. Total Cost = sum of costs for all lines

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:

 Line1: "omg "very"


 Line2: "are"
 Line3: "extreme"

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:

 1 <= number of words <= 103


 1 <= limit <= 15 * 103
 1 <= length of a word <= 103
 Given words are composed of lowercase and uppercase English alphabet characters.

Word Wrap

def solve_balanced_line_breaks(words, limit):


"""
Args:
words(list_str)
limit(int32)
Returns:
int64
"""
# Write your code here.
return 0

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:

 len(i) = len(a) + len(b).


 i only contains characters present in a or b.
 i contains all characters of a. From a, any character a[index] should be added exactly once in i.
 i contains all characters of b. From b, any character b[index] should be added exactly once in i.

Order of all characters in individual strings ( a and b) is preserved.

Example One
{
"a": "123",
"b": "456",
"i": "123456"
}
Output:

true

Example Two
{
"a": "AAB",
"b": "AAC",
"i": "AAAABC"
}
Output:

true

Notes

Input strings can contain:

 Small alphabets - 'a' to 'z'


 Large alphabets - 'A' to 'Z'
 Numbers - 0 to 9

Constraints:

 1 <= len(a), len(b) <= 102


 1 <= len(i) <= 2* 102

def do_strings_interleave(a, b, i):


"""
Args:
a(str)
b(str)
i(str)
Returns:
bool
"""
# Write your code here.
return False

Longest Common Subsequence


Find the longest common subsequence of two strings.

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

 If a nonempty common subsequence cannot be found, return "-1".

Constraints:

 1 <= length of each of the input strings <= 400


 Input strings consist of the alphanumeric characters

Longest Common Subsequence - Omkar Deshpande

def lcs(a, b):


"""
Args:
a(str)
b(str)
Returns:
str
"""
# Write your code here.
return ''

Problem

Matrix Chain Multiplication


Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is
not actually to perform the multiplications, but merely to decide in which order to perform the
multiplications.

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:

 3 <= length of matrix_sizes <= 100


 0 <= matrix_sizes[i] <= 100, for all i.

Matrix Chain Multiplication

def minimum_multiplication_cost(matrix_sizes):
"""
Args:
matrix_sizes(list_int32)
Returns:
int32
"""
# Write your code here.
return 0

You might also like