Thursday, May 15, 2025

Discussion on Cost of Balloons (Problem from HackerEarth)

To See All Articles About Technology: Index of Lessons in Technology
See the question on Hacker Earth
Difficulty: Easy

Problem

You are conducting a contest at your college. This contest consists of two problems and n participants. You know the problem that a candidate will solve during the contest.

You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize for solving that specific problem. You can distribute balloons to each participant by performing the following operation:

Use green-colored balloons for the first problem and purple-colored balloons for the second problem
Use purple-colored balloons for the first problem and green-colored balloons for the second problem
You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons.

Input format

First line: T that denotes the number of test cases (1 <= T <= 10)
For each test case: 
First line: Cost of green and purple-colored balloons 
Second line: n that denotes the number of participants (1 <= n <= 10)

Next n lines: Contain the status of users. For example, if the value of the jth integer in the ith row is 0, then it depicts that the ith participant has not solved the jth problem. Similarly, if the value of the jth integer in the ith row is 1, then it depicts that the ith participant has solved the jth problem.
Output format
For each test case, print the minimum cost that you have to pay to purchase balloons.

Sample Input
2
9 6
10
1 1
1 1
0 1
0 0
0 1
0 0
0 1
0 1
1 1
0 0
1 9
10
0 1
0 0
0 0
0 1
1 0
0 1
0 1
0 0
0 1
0 0
Sample Output
69
14
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
-

My Solution

T = int(input()) # num of test cases

for i in range(T):
    costs = input().split()
    costs = [int(c) for c in costs]

    min_cost = 0
    max_cost = 0
    if(costs[0] >= costs[1]):
        max_cost = costs[0]
        min_cost = costs[1]
    else:
        max_cost = costs[1]
        min_cost = costs[0]

    n = int(input()) # num of participants

    x_cnt = 0
    y_cnt = 0

    for j in range(n):
        status = input().split()
        if(status[0] == '1'):
            x_cnt += 1
        if(status[1] == '1'):
            y_cnt += 1

    if(x_cnt <= y_cnt):
        print(x_cnt * max_cost + y_cnt * min_cost)
    else:
        print(x_cnt * min_cost + y_cnt * max_cost)

Critique

Your solution works well and produces the correct output for the given problem. Let's evaluate it in terms of correctness, readability, and opportunities for improvement.


Problem Understanding (Recap)

Each participant may solve:

  • Problem 1: earns a balloon of one color.

  • Problem 2: earns a balloon of another color.

You can:

  • Assign green to problem 1 and purple to problem 2, or

  • Assign purple to problem 1 and green to problem 2.

The goal is to minimize the total cost of the balloons, depending on which problem participants solved and the balloon cost.


Correctness

Your code correctly:

  1. Reads costs and number of participants.

  2. Counts how many times each problem was solved (x_cnt, y_cnt).

  3. Chooses the cheaper mapping between balloon colors and problems.

It produces correct results for the provided test cases. ✅


๐Ÿ” Critique & Suggestions

1. ❌ Misleading variable naming: max_cost and min_cost

  • You’re not actually comparing costs to pick min/max based on which problem was solved more often.

  • What you should do is compute two total costs and pick the minimum.

Improvement: Calculate both options directly and pick the cheaper one.

python
T = int(input()) for _ in range(T): cost_g, cost_p = map(int, input().split()) n = int(input()) problem1 = 0 problem2 = 0 for _ in range(n): a, b = map(int, input().split()) problem1 += a problem2 += b # Option 1: green for problem1, purple for problem2 cost_option1 = problem1 * cost_g + problem2 * cost_p # Option 2: purple for problem1, green for problem2 cost_option2 = problem1 * cost_p + problem2 * cost_g print(min(cost_option1, cost_option2))

2. ๐Ÿ”„ Redundant logic for determining cheaper color

  • Your approach compares costs and conditionally assigns min_cost and max_cost, then compares counts, which is logically flawed in some edge cases.

Better to always calculate both cost combinations explicitly, as shown above.

3. ๐Ÿงผ Readability

  • Rename x_cntproblem1

  • Rename y_cntproblem2

It improves clarity and aligns with the problem.


๐Ÿงฎ Complexity Analysis

⏱ Time Complexity:

  • For each test case: O(n)

  • Total: O(T * n)

    • With T ≤ 10, n ≤ 10 → worst case = 100 lines = trivial runtime

๐Ÿง  Space Complexity:

  • O(1) (only counters and a few integers used)


✅ Summary

AspectRating / Comment
✅ Correctness✅ Works perfectly
❌ Efficiency✅ Fine due to low constraints
❌ ClarityVariable naming is misleading
❌ FlexibilityHardcoded min/max assignment logic is unnecessary
✅ ReadabilityCould be improved
✅ Suggested FixCompute both cost options and pick minimum

๐Ÿ”„ Final Suggestion: Use this simplified version

python
T = int(input()) for _ in range(T): cost_g, cost_p = map(int, input().split()) n = int(input()) problem1 = 0 problem2 = 0 for _ in range(n): a, b = map(int, input().split()) problem1 += a problem2 += b cost1 = problem1 * cost_g + problem2 * cost_p cost2 = problem1 * cost_p + problem2 * cost_g print(min(cost1, cost2))

Let me know if you’d like this converted to another language (e.g., C++ or Java) or want to add test case automation.

Tags: Technology,Algorithms,

No comments:

Post a Comment