Crafter
This is an interactive problem.
Note: This problem statement, theme, and framing have been substantially paraphrased, and the problems have been merged and extended.
Implement the class CraftingEngine, which simulates a crafting system on the MMORPG server Wynncraft. In this game, players craft weapons by imbuing crafting materials into a base weapon. Each material contributes some damage to the weapon, but at the cost of consuming some of the weapon's durability.
You will receive a stream of crafting materials, each described by a durability cost and a damage contribution. Your engine must support querying the maximum damage achievable given a durability budget, as well as tracking potentially unstable crafting sequences.
A material is considered poor if its durability cost exceeds its damage contribution. A crafting sequence becomes unstable when it contains threshold or more consecutive poor materials — at which point the weapon must be replaced. Your engine should be able to count how many such replacements have occurred across all materials added so far.
Methods
CraftingEngine(base_durability)
base_durabilityis a positive integer representing the weapon's starting durability before any materials are applied.
add(cost, damage)
- Adds a new crafting material with durability
costanddamagecontribution to the engine's internal list. costanddamageare both non-negative integers.
countReplacements(threshold)
- Returns the number of weapon replacements that would occur among all materials added so far.
- A material is poor if its
coststrictly exceeds itsdamage. - A weapon must be replaced each time a run of at least
thresholdconsecutive poor materials is encountered. After a replacement, the consecutive count resets — subsequent poor materials begin a new count. thresholdis a positive integer.
maximumDamage(min_durability)
- Returns the maximum total damage achievable by selecting any subset of materials added so far, such that the weapon's remaining durability is at least
min_durability. - The weapon starts with
base_durability(provided at construction). Each selected material reduces the durability by itscostand increases the total damage by itsdamage. - Poor materials are not excluded from selection, and a subset that would be considered unstable is still valid — the only constraint is the durability budget.
min_durabilityis a non-negative integer.- If no valid selection exists, return
0.
Notes
- Query operations do not need to be performed in constant time.
- You do not need to implement any input/output parsing.
Example
Suppose base_durability = 10, and the following calls are made:
add(3, 2) # material 0: cost=3, damage=2 → poor (3 > 2)
add(1, 5) # material 1: cost=1, damage=5 → not poor
add(4, 1) # material 2: cost=4, damage=1 → poor
add(2, 1) # material 3: cost=2, damage=1 → poor
add(1, 3) # material 4: cost=1, damage=3 → not poor
countReplacements(2) → 1
The poor materials are at indices 0, 2, 3. Index 0 is isolated (run of length 1 < threshold, no replacement). Indices 2–3 form a consecutive run of length 2 ≥ threshold, triggering 1 replacement. Total: 1.
maximumDamage(5) → 8
We need the remaining durability ≥ 5, meaning the total cost of selected materials must be ≤ 10 − 5 = 5. We may pick any subset. Some candidates:
| Selection | Total Cost | Total Damage | Valid? |
|---|---|---|---|
{1} |
1 | 5 | ✓ |
{4} |
1 | 3 | ✓ |
{1, 4} |
2 | 8 | ✓ |
{1, 3} |
3 | 6 | ✓ |
{0, 1} |
4 | 7 | ✓ |
{1, 3, 4} |
4 | 9 | ✓ |
{0, 1, 4} |
5 | 10 | ✓ |
{1, 2, 4} |
6 | 9 | ✗ |
{0, 1, 3, 4} |
7 | 11 | ✗ |
The maximum valid damage is 10 from selection {0, 1, 4} (materials 0, 1, and 4), with total cost 3+1+1=5 ≤ 5. → returns 10.
Constraints
-
base_durability - The total number of
addcalls will not exceed -
cost,damage -
thresholdtotal materials added at time of query -
min_durabilitybase_durability
Running with data stream...