DIY: Divide Chocolate
Solve the interview question "Divide Chocolate" in this lesson.
We'll cover the following
Problem statement
You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the array sweetness
.
You have s
friends that you want to share the chocolate with. You cut the chocolate using k
cuts and get k + 1
pieces, where each piece consists of consecutive chunks.
Since you are the host, you will eat the piece with the minimum total sweetness and give the other pieces to your friends.
Find the maximum total sweetness of the piece you can get by cutting the chocolate bar optimally.
Input
The input will be a list of integers representing the sweetness of chocolate pieces and an integer value of k
. The following is an example input:
sweetness = [1, 2, 3, 4, 5]
k = 3
Output
The output will be an integer value representing the maximum sweetness we can get from the list. The following is an example output for the above input:
3
The chocolate can be divided into [1,2]
, [3]
, [4]
, and [5]
. We can either get the part [1,2]
or [3]
.
Coding exercise
Implement the maximize_sweetness(chocolate, k)
function, where sweetness
is the list of integers and k
is the integer number that will divide the chocolate. The function will return a single integer value representing the amount of sweetness the host would get.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.