Data Structures & AlgorithmsVery hardProduct Company SDE · DP round
Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP.
Problem: What is the maximum product of positive integer parts when 10 is split into at least two parts? Run the DP.
n = 10
dp = [0] * (n + 1)
dp[1] = 1
for m in range(2, n + 1):
best = m - 1
for k in range(1, m):
best = max(best, k * dp[m - k], k * (m - k))
dp[m] = best
print(dp[n])What is printed?- A. 32
- B. 36
- C. 30
- D. 27
Correct answer: B. 36
Splitting into 3 + 3 + 4 gives 36, and 2 + 2 + 3 + 3 also gives 36; parts of size 1 never help, so 36 is the maximum.
More Coding Interview Problems questions
- Problem: How many integer partitions does 6 have (unordered sums of positive integers equal to 6)? Run the DP. python n = 6 p = [1] + [0]…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 10 have x y <= 10? Run the code. python print(sum(1 for x in range(1, 11) for y…
- Problem: How many integers in [1, 1000] contain the digit 0? Run the code. python print(sum(1 for x in range(1, 1001) if '0' in str(x)))…
- Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP. python INF = 10 9 dp = [0] + [INF] 12…
- Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code. python from itertools import product…
- Problem: What is the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5]? Run the DP. python a = [1, 101, 2, 3…