Maximize scheduler profit without running two consecutive processes

Quick Overview

Given a queue of process profits, some of them negative, pick which processes to run so that no two adjacent ones run and the total profit is as large as possible. It tests one-dimensional dynamic programming, the base case for an empty selection, and cutting the table down to constant space.

Maximize scheduler profit without running two consecutive processes

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are an operating system scheduler with a queue of processes lined up in a specific order. Each process has an associated profit value, which may be negative if running it is costly. You must decide which processes to execute to maximize total profit, subject to one rule: you cannot execute two consecutive processes in the queue (running back-to-back processes would overload the CPU). Given an integer array `profits` where `profits[i]` is the profit gained by running the i-th process, return the maximum total profit you can obtain. You may also choose to run no processes at all, which yields a profit of `0`. ### Function Signature ```python def max_profit(profits: list[int]) -> int: ``` ### Rules - Two processes are consecutive when their indices differ by 1. Any set of indices with no two adjacent is allowed, including the empty set. - Return the total profit of the best allowed set. Because the empty set is allowed, the answer is never below `0`. ### Constraints - `1 <= len(profits) <= 10^5` - `-10^4 <= profits[i] <= 10^4` ### Examples ```text Input: profits = [3, 2, 5, 10, 7] Output: 15 ``` Running indices 0, 2 and 4 gives 3 + 5 + 7 = 15. Any set that uses the 10 at index 3 must skip indices 2 and 4, so it reaches at most 3 + 10 = 13. ```text Input: profits = [5, 1, 1, 5] Output: 10 ``` Indices 0 and 3 are not adjacent, so both 5s can run for 10. Any set that includes a 1 can hold only one of the 5s, which gives at most 6. ```text Input: profits = [-5, -3, -8] Output: 0 ``` Every process loses money, so the best choice is to run nothing.

Overview: Given a queue of process profits, some of them negative, pick which processes to run so that no two adjacent ones run and the total profit is as large as possible. It tests one-dimensional dynamic programming, the base case for an empty selection, and cutting the table down to constant space.

Citadel logo
Citadel
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are an operating system scheduler with a queue of processes lined up in a specific order. Each process has an associated profit value, which may be negative if running it is costly. You must decide which processes to execute to maximize total profit, subject to one rule: you cannot execute two consecutive processes in the queue (running back-to-back processes would overload the CPU).

Given an integer array profits where profits[i] is the profit gained by running the i-th process, return the maximum total profit you can obtain. You may also choose to run no processes at all, which yields a profit of 0.

Function Signature

def max_profit(profits: list[int]) -> int:

Rules

  • Two processes are consecutive when their indices differ by 1. Any set of indices with no two adjacent is allowed, including the empty set.
  • Return the total profit of the best allowed set. Because the empty set is allowed, the answer is never below 0 .

Constraints

  • 1 <= len(profits) <= 10^5
  • -10^4 <= profits[i] <= 10^4

Examples

Input: profits = [3, 2, 5, 10, 7]
Output: 15

Running indices 0, 2 and 4 gives 3 + 5 + 7 = 15. Any set that uses the 10 at index 3 must skip indices 2 and 4, so it reaches at most 3 + 10 = 13.

Input: profits = [5, 1, 1, 5]
Output: 10

Indices 0 and 3 are not adjacent, so both 5s can run for 10. Any set that includes a 1 can hold only one of the 5s, which gives at most 6.

Input: profits = [-5, -3, -8]
Output: 0

Every process loses money, so the best choice is to run nothing.