Crafter

10s 256 MB
Easy+ 2 AlgorithmsBasics Visa

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_durability is a positive integer representing the weapon's starting durability before any materials are applied.

add(cost, damage)

  • Adds a new crafting material with durability cost and damage contribution to the engine's internal list.
  • cost and damage are 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 cost strictly exceeds its damage.
  • A weapon must be replaced each time a run of at least threshold consecutive poor materials is encountered. After a replacement, the consecutive count resets — subsequent poor materials begin a new count.
  • threshold is 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 its cost and increases the total damage by its damage.
  • 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_durability is 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

  • 11 \leq base_durability 104\leq 10^4
  • The total number of add calls will not exceed 10310^3
  • 00 \leq cost, damage 104\leq 10^4
  • 11 \leq threshold \leq total materials added at time of query
  • 00 \leq min_durability \leq base_durability
Accepted 1/3
Acceptance 33%
Loading editor...
Input
Running with data stream...
Expected Output