-3

I am trying the problem : 1833. Maximum Ice Cream Bars from Leetcode:

It is a sweltering summer day, and a boy wants to buy some ice cream bars. At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible. Return the maximum number of ice cream bars the boy can buy with coins coins.

Note: The boy can buy the ice cream bars in any order.

Example 1:

Input: costs = [1,3,2,4,1], coins = 7
Output: 4
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.

I am using while loop and in the code as follows:

class Solution {
public:
    int maxIceCream(vector<int>& costs, int coins) {
        long long int count = 0;
        sort(costs.begin(), costs.end());
        int i = 0;
        while(coins > 0 && i < costs.size())
        {
            count++;
            coins -= costs[i];
            i++;
        }
        return coins;
    }
};

I get the following error. Can someone please help why I get this? The code works fine with for loop. Line 12: Char 19: runtime error: signed integer overflow: 1094795588 - -1094795586 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:21:19

The constraints are given as follows:

costs.length == n
1 <= n <= 10^5
1 <= costs[i] <= 10^5
1 <= coins <= 10^8
rustyx
  • 80,671
  • 25
  • 200
  • 267
  • 2
    `.size()` returns `size_t` so just use that as type for `i` – Adriano Repetti Jan 06 '23 at 10:28
  • 1
    why is there `count` and `i` ? If everything goes well they should have the same value – 463035818_is_not_an_ai Jan 06 '23 at 10:29
  • 2
    you need to write your own tests. You need to run the code in your local environment where you can attach a debugger. Thats how one fixes bugs. You dont fix bugs by letting someoneelse do the compiling and testing for you. Online code judges are made to not let you know how to fix your code, but you need the opposite of that. Start by creating a [mcve], so you can reproduce the problem with known input – 463035818_is_not_an_ai Jan 06 '23 at 10:40
  • 1
    He has 7 coins and could technically purchase a 7 bars of the first type of ice cream for 1 coin each. I suppose the problem is implicitly saying the boy can't buy then more than one of each item. – selbie Jan 06 '23 at 10:53
  • @selbie I think greedy should work because I solved the same problem using for loop and it passed all test cases but it fails with the while loop :( – Ojaswi Awasthi Jan 06 '23 at 10:57
  • Just a question, are you trying to learn C++ from leetcode, or prepare for a job interview, or just having fun. Because honestly it is a bad bad site to learn C++ from. And to prepare for a job interview knowledge about design/unit testing is worth far more. – Pepijn Kramer Jan 06 '23 at 13:54
  • When you asked this question on the LeetCode forums, what answers did you get? – Eljay Jan 06 '23 at 14:36
  • @OjaswiAwasthi - you are correct. The goal is to maximize the number of items, not the maximum worth of items. Hence, the greedy solution works better. – selbie Jan 06 '23 at 23:46

1 Answers1

1

In the while loop, you should not check if the money is greater than zero, but you should check if buying another ice cream is possible if you don't do it, you may spend more money than you have

while(coins - costs[i] > 0 && i < costs.size())

it's worth noting that you're returning the amount of coins left, not the amount of ice cream that was purchased

Darkolin
  • 71
  • 5
  • 2
    I'd write a function called `bool can_buy_more_icecreams(...)`. So that the code would read `while( can_buy_more_icecreams(...))` which tells the story in code. It is also one of the reaons I really don't like leetcode, it really doesn't teach people how to write maintainable code (or even good C++ for that matter) – Pepijn Kramer Jan 06 '23 at 13:57