Lowest Point on the Trail
A hiking app records your route as a list of waypoint altitudes. Walking forward adds a waypoint. Retracing your steps from a dead end drops the waypoints you walk back over, so the list is always your path from the trailhead.
The screen shows your current altitude and the lowest altitude on your current path, refreshed after every step, so both values must be available instantly, even with thousands of waypoints.
Your task
Write a class TrailLog:
step(altitude)adds a waypoint at the end of the path.back()removes the most recent waypoint.current()returns the altitude of the most recent waypoint.lowest()returns the lowest altitude among all waypoints still on the path.
Every method must run in O(1) time.
class TrailLog
Examples
new TrailLog() .step(340) .step(280) .step(410) .lowest() .back() .back() .lowest() .current()
[null, null, null, null, 280, null, null, 340, 340]
The hiker walks to 340 m, 280 m and then 410 m, so the lowest altitude is 280. Backing off 410 does not change the lowest, but backing off 280 as well leaves only 340, so both lowest and current return 340.
Constraints
- 1 ≤ number of method calls ≤ 1,000
- -1,000 ≤ altitude ≤ 10,000 (metres; negative means below sea level). Altitudes may repeat.
- back, current and lowest are only called when the path has at least one waypoint.
Start with brute force
Keep altitudes in an array and scan it on every lowest() call. That is O(n) per call and O(n²) over a hike of n waypoints.
A single low variable is fast but wrong: it cannot recover the previous low after back() removes the lowest waypoint.
Hints
0 of 4 shown- Hint 1
Suppose you walk to 500 and then to 200, so the lowest is 200, and then you back off 200. If you stored only one variable
low = 200, how would you know the new lowest is 500? - Hint 2
After the lowest waypoint is removed, the answer is the lowest the path had before that waypoint was added. That value must still be stored somewhere.
- Hint 3
The path only changes at its end, so it behaves like a stack. What if every entry also remembered the lowest altitude up to and including itself?
- Hint 4
Push pairs
(altitude, lowestSoFar), wherelowestSoFar = min(altitude, lowestSoFar of the entry below).backpops a pair,currentreads the top altitude,lowestreads the top'slowestSoFar.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The path changes only at its end, so it is a stack. The hard part is lowest() after a back(). To solve it, store with each waypoint the lowest altitude of the path at that moment. Popping then restores the old answer, because the entry below still holds it.
- step 340 has no entry below it, so push
(340, 340). - step 280 gives min(280, 340) = 280, so push
(280, 280). - step 410 gives min(410, 280) = 280, so push
(410, 280). lowest() reads the top entry and returns 280. - The two back calls pop
(410, 280)and then(280, 280), so the top is now(340, 340). - lowest() and current() both return 340.
class TrailLog {
constructor() {
this.path = []; // entries: [altitude, lowest altitude up to here]
}
step(altitude) {
const below = this.path.length ? this.path[this.path.length - 1][1] : altitude;
this.path.push([altitude, Math.min(altitude, below)]);
}
back() {
this.path.pop();
}
current() {
return this.path[this.path.length - 1][0];
}
lowest() {
return this.path[this.path.length - 1][1];
}
}class TrailLog:
def __init__(self):
self.path = [] # entries: (altitude, lowest altitude up to here)
def step(self, altitude):
below = self.path[-1][1] if self.path else altitude
self.path.append((altitude, min(altitude, below)))
def back(self):
self.path.pop()
def current(self):
return self.path[-1][0]
def lowest(self):
return self.path[-1][1]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.