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

Sometimes we need to maximize the number of happy customers in a store by strategically changing their moods. This problem involves finding the optimal sublist of customers to make happy using a sliding window approach.

Problem Statement

We have two lists of equal length: customers and mood, plus an integer k. At minute i, customers[i] people enter the store. When mood[i] = 1, customers are happy; when mood[i] = 0, they are sad. We can change at most k consecutive sad customers to happy and find the maximum total happy customers.

Example

If customers = [2, 3, 6, 6, 3], mood = [1, 1, 0, 0, 0], and k = 2, we can change positions 2 and 3 to get [1, 1, 1, 1, 0], making 2 + 3 + 6 + 6 = 17 customers happy.

Algorithm Steps

  • Count customers who are already happy
  • Create a prefix sum array for sad customers
  • Use sliding window to find maximum gain from changing k consecutive positions
  • Return initial happy customers plus maximum possible gain

Implementation

def solve(customers, mood, k):
    n = len(mood)
    prefix_sum = [0] * (n + 1)
    already_happy = 0
    
    # Build prefix sum for sad customers and count already happy
    for i in range(n):
        prefix_sum[i + 1] = prefix_sum[i]
        if mood[i]:
            already_happy += customers[i]
        else:
            prefix_sum[i + 1] += customers[i]
    
    # Find maximum gain by changing k consecutive sad customers
    max_gain = 0
    for i in range(k, n + 1):
        gain = prefix_sum[i] - prefix_sum[i - k]
        max_gain = max(max_gain, gain)
    
    return already_happy + max_gain

# Test the solution
customers = [2, 3, 6, 6, 3]
mood = [1, 1, 0, 0, 0]
k = 2
result = solve(customers, mood, k)
print(f"Maximum happy customers: {result}")
Maximum happy customers: 17

How It Works

The algorithm uses a prefix sum array to efficiently calculate the sum of any subarray of sad customers. For each possible window of size k, it calculates how many additional customers would become happy if we change their mood from sad to happy.

Step-by-Step Trace

def solve_with_trace(customers, mood, k):
    n = len(mood)
    prefix_sum = [0] * (n + 1)
    already_happy = 0
    
    print(f"Input: customers = {customers}, mood = {mood}, k = {k}")
    
    # Build prefix sum and count happy customers
    for i in range(n):
        prefix_sum[i + 1] = prefix_sum[i]
        if mood[i]:
            already_happy += customers[i]
            print(f"Position {i}: {customers[i]} customers already happy")
        else:
            prefix_sum[i + 1] += customers[i]
            print(f"Position {i}: {customers[i]} customers sad")
    
    print(f"Already happy customers: {already_happy}")
    print(f"Prefix sum array: {prefix_sum}")
    
    # Find best window
    max_gain = 0
    best_start = -1
    for i in range(k, n + 1):
        gain = prefix_sum[i] - prefix_sum[i - k]
        print(f"Window [{i-k}:{i}] can make {gain} customers happy")
        if gain > max_gain:
            max_gain = gain
            best_start = i - k
    
    print(f"Best window starts at position {best_start} with gain {max_gain}")
    return already_happy + max_gain

customers = [2, 3, 6, 6, 3]
mood = [1, 1, 0, 0, 0]
k = 2
result = solve_with_trace(customers, mood, k)
print(f"Total maximum happy customers: {result}")
Input: customers = [2, 3, 6, 6, 3], mood = [1, 1, 0, 0, 0], k = 2
Position 0: 2 customers already happy
Position 1: 3 customers already happy
Position 2: 6 customers sad
Position 3: 6 customers sad
Position 4: 3 customers sad
Already happy customers: 5
Prefix sum array: [0, 0, 0, 6, 12, 15]
Window [0:2] can make 0 customers happy
Window [1:3] can make 6 customers happy
Window [2:4] can make 12 customers happy
Window [3:5] can make 9 customers happy
Best window starts at position 2 with gain 12
Total maximum happy customers: 17

Time and Space Complexity

Time Complexity: O(n) where n is the length of the arrays, as we iterate through the arrays once to build the prefix sum and once to find the maximum window.

Space Complexity: O(n) for the prefix sum array.

Conclusion

This problem is solved using a sliding window technique with prefix sums. We count already happy customers and find the optimal consecutive window of sad customers to convert, maximizing the total happy customers efficiently.

Updated on: 2026-03-26T16:46:49+05:30

621 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements