DIEHARD -DIE HARD
Pattern: Candy and assign cookies
You start with some health and armour and move between air (+3 health, +2 armour), water (-5 health, -10 armour) and fire (-20 health, +5 armour), never staying in one place twice in a row. Air only helps, so go to air every other step; between, take water if it is safe, else fire, else stop.
The problem
Each unit of time you move to one of three places, never the one you are in: air adds 3 health and 2 armour, water takes 5 health and 10 armour, fire takes 20 health and adds 5 armour. If health or armour reaches 0 or below you die. Return the longest time you can survive, counting each move you survive. Health 20 and armour 8 give 5. The linked page could not be read when this guide was written, so the rules below are this guide's own statement of the task.
Think first
- Is air ever a bad move?
- After air, where can you go?
- Between water and fire, which costs less of what matters?
Hints
Brute force, and why it fails
Try every sequence with memo over (health, armour, place): the tests use exactly this to check the greedy.
The key insight
Since air only helps and you cannot stay, the best plan alternates air with one of the others; taking water when possible saves health, the scarcer resource.
Dry run
| health | 23 |
|---|---|
| armour | 10 |
| time | 1 |
Health 20, armour 8. Air: 23, 10 (time 1).
The plan
loop: air (+3, +2), time += 1
if health > 5 and armour > 10: water
elif health > 20: fire
else: return time
time += 1
Complexity
- Time
O(answer)- Space
O(1)
Each loop is two moves.
Edge cases
- Tiny health and armour: 1 (one move to air).
- Lots of armour, little health.
- Lots of health, little armour.
I've built my logic, show the code
Java
class Solution {
public int survive(int health, int armour) {
int time = 0;
while (true) {
health += 3; // air first: it only helps
armour += 2;
time++;
if (health > 5 && armour > 10) { // water costs less health than fire
health -= 5;
armour -= 10;
} else if (health > 20) {
health -= 20;
armour += 5;
} else {
return time; // neither is safe: the run ends in the air
}
time++;
}
}
}C++
class Solution {
public:
int survive(int health, int armour) {
int time = 0;
while (true) {
health += 3; // air first: it only helps
armour += 2;
time++;
if (health > 5 && armour > 10) { // water costs less health than fire
health -= 5;
armour -= 10;
} else if (health > 20) {
health -= 20;
armour += 5;
} else {
return time; // neither is safe: the run ends in the air
}
time++;
}
}
};Python
class Solution:
def survive(self, health: int, armour: int) -> int:
time = 0
while True:
health, armour, time = health + 3, armour + 2, time + 1 # air first: it only helps
if health > 5 and armour > 10: # water costs less health than fire
health, armour = health - 5, armour - 10
elif health > 20:
health, armour = health - 20, armour + 5
else:
return time # neither is safe: the run ends in the air
time += 1Common mistakes
- Starting with water or fire instead of air.
- Allowing health or armour to reach exactly 0.