Crafter

Easy+ 2 10s 256 MB
BasicsAlgorithms 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, and the weapon must then be replaced. Your engine should be able to count how many such replacements have occurred across all materials added so far.

Signature
CraftingEngine(base_durability)
  • base_durability is a positive integer representing the weapon's starting durability before any materials are applied.
Signature
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.
Signature
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 and the next poor material starts a new count.
  • threshold is a positive integer.
Signature
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 can be selected, and a subset that would be 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.
  • Query operations do not need to be performed in constant time.
  • You do not need to implement any input/output parsing.

Suppose base_durability = 10, and the following calls are made:

Text
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) → 10

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.

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