
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find maximum number of people we can make happy in Python
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
- Related Articles
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of consecutive values you can make in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to find maximum number of courses we can take based on interval time in Python?
- Program to find number of distinct coin sums we can make with coins and quantities in Python?
- Program to count number of strings we can make using grammar rules in Python
- Program to find minimum number of people to teach in Python
- Program to find maximum score we can get in jump game in Python
- Program to count number of unique palindromes we can make using string characters in Python

Advertisements