You’re building a mobile fintech app with 10M+ DAUs where users can run a “portfolio risk simulation” that triggers thousands of CPU-heavy calculations. On mid-range devices, running these calculations in one burst causes the UI to freeze, dropping frames and risking user abandonment during high-stakes flows (e.g., placing a trade).
To keep the UI responsive, your runtime provides a cooperative scheduler: the UI thread can spend at most frame_budget_ms milliseconds per frame on background work. Any leftover work must be deferred to later frames.
You are given a list of independent calculation tasks, where tasks[i] is the time in milliseconds required to complete task i (each task must run contiguously; you cannot split a single task across frames).
Write a function:
tasks: list[int], frame_budget_ms: int-1 if it’s impossible (i.e., any single task exceeds the frame budget).Within a frame, you may run any subset of remaining tasks as long as their total time is <= frame_budget_ms.
tasks is empty, return 0.tasks[i] > frame_budget_ms, return -1.0 <= len(tasks) <= 2 * 10^51 <= frame_budget_ms <= 10^91 <= tasks[i] <= 10^9