TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Laundry

Problem statement, definition, constraints, and public examples.

Problem Statement

As a college student, I hate doing laundry. I'll wear every single shirt I own before I wash any of them. Unfortunately, doing laundry is unavoidable and I end up doing it from time to time. However, I can reduce the frequency with which I must do laundry by buying more shirts (we'll ignore the other articles of clothing in this problem). I don't want to buy too many shirts though, because it would be too expensive. Each shirt I buy costs costPerShirt and I have determined that I can wash all my shirts (regardless of how many I have) in an amount of time that I value at costPerWash (in the same units as costPerShirt). My goal is to minimize the cost of wearing a clean shirt every day (a shirt is no longer clean after it has been worn for one day). The cost can be found as costPerShirt*shirtsPurchased + costPerWash*numberOfWashes.

Furthermore, due to the ever increasing rate at which fashion trends come and go in the modern world, I expect all of my shirts to last one non-leap year -- 365 days. I'm in the process of purchasing my wardrobe for the upcoming year, and need your help. Your task is to write a method that, given costPerWash and costPerShirt, returns an int telling me how many shirts to buy to minimize my cost. If there is more than one number that gives me the optimal cost, return the largest one. Also, I'm willing to wear shirts right after I buy them, without washing them first. Finally, you may assume that I always wash my shirts at night, and that I can wash all of my shirts (including the one that I just finished wearing).

For example, lets say that costPerWash = 10 and costPerShirt = 25. Then, if I buy 12 shirts, I'll need to wash them 30 times, for a cost of 30*10+25*12 = 600. If I had bought 13 shirts instead, I would have needed to wash them 28 times, for a cost of 28*10+25*13 = 605. If I had bought 11 shirts, it would have cost me 33*10+25*11 = 605. Other numbers also produce costs greater than 600, hence the return is 12.

Definition

Class:
Laundry
Method:
optimalNumber
Parameters:
int, int
Returns:
int
Method signature:
int optimalNumber(int costPerWash, int costPerShirt)
(be sure your method is public)

Notes

  • Assume that I can wash all of my shirts at once. So, if I buy 5 shirts, I can wear one each for the first 5 days. Then I can wash them between the 5th and 6th days, and wear one each for days 6-10, inclusive.

Constraints

  • costPerWash will be between 1 and 1000, inclusive.
  • costPerShirt will be between 1 and 1000, inclusive.

Examples

  1. 10
    25
    Returns: 12
    From the problem statement.
  2. 100
    25
    Returns: 41
  3. 3
    50
    Returns: 5
    If I buy 5 shirts, I must wash them 72 times, for a cost of 3*72*50*5 = 466.
  4. 25
    25
    Returns: 23
    22 shirts cost the same amount as 23 shirts (905), so we return 23.
Back to all problems