# Program to find maximum number of people we can make happy in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a list customers and another list mood, these two are of same length, we also have another integer k. Now on each minute i, customers[i] number of people come to the store and when mood[i] = 1, it indicates the customers are happy and when mood[i] = 0, then they are sad. We can set a sublist of size k of mood to 1s, finally we have to find the maximum number of people we can make happy.

So, if the input is like customers = [2, 3, 6, 6, 3] mood = [1, 1, 0, 0, 0] k = 2, then the output will be 17 because if we set mood[2] and mood[3] to 1, then the total mood will be 2 + 3 + 6 + 6 = 17 customers happy.

To solve this, we will follow these steps −

• n := size of mood
• a := a list of size (n + 1) and fill with 0
• s := 0
• for i in range 0 to n - 1, do
• a[i + 1] := a[i]
• if mood[i] is non-zero, then
• s := s + customers[i]
• otherwise,
• a[i + 1] := a[i + 1] + customers[i]
• d := 0
• for i in range k to n, do
• d := maximum of d and (a[i] - a[i - k])
• return s + d

## Example

Let us see the following implementation to get better understanding −

def solve(customers, mood, k):
n = len(mood)
a = [0] * (n + 1)
s = 0
for i in range(n):
a[i + 1] = a[i]
if mood[i]:
s += customers[i]
else:
a[i + 1] += customers[i]
d = 0
for i in range(k, n + 1):
d = max(d, a[i] - a[i - k])
return s + d

customers = [2, 3, 6, 6, 3]
mood = [1, 1, 0, 0, 0]
k = 2
print(solve(customers, mood, k))

## Input

[2, 3, 6, 6, 3], [1, 1, 0, 0, 0], 2


## Output

17
Updated on 14-Oct-2021 12:50:52