📱Recent Apps List!
📱 Picture your phone's recent apps screen. • Open an app → moves to the front • Low memory → close the oldest app • By app name → find and switch instantly Keeping only the N most recent is exactly LRU!
Loading...
📱 Picture your phone's recent apps screen. • Open an app → moves to the front • Low memory → close the oldest app • By app name → find and switch instantly Keeping only the N most recent is exactly LRU!
You are building the background app manager for a smartphone. Memory is tight, so at most N recently used apps can stay alive. The user performs two actions: - launch(app, screen): Launch (or reopen) an app, remembering the screen number it was on. If the app is already alive, update its screen number and mark it 'most recently used'. If it is a new app and N apps are already alive, terminate the least recently used app (free its memory) and bring up the new one. - switchTo(app): Switch to the app. If it is alive, show the remembered screen number (and it becomes 'most recently used'); if it has already been terminated, return -1 (it must be restarted from the first screen). Given a sequence of actions, output the result of each switchTo in order, then the final memory state.
N = 3
launch("MAP", 1)
launch("CAM", 2)
launch("MSG", 3)
switchTo("MAP")
launch("WEB", 4)
switchTo("PAY")
switchTo("MSG")
launch("CAM", 5)switchTo("MAP") → 1
switchTo("PAY") → -1
switchTo("MSG") → 3
메모리: CAM, MSG, WEB (최근→오래된)Switching to MAP promotes it to most recently used. Launching WEB fills memory, so the oldest CAM is terminated. PAY is not alive, so -1. The final launch of CAM fills memory again, terminating the oldest MAP, leaving memory as CAM, MSG, WEB from most recent.