Appearance
CPU Core Allocation Problem
You are designing a special CPU scheduler for a multi-core system that needs to allocate processing cores based on request priorities. Each process has a priority level, and you must follow these strict scheduling rules:
- Every process must be assigned at least one CPU core
- A process with higher priority than its neighbor must receive more cores
Your goal is to determine the minimum total number of CPU cores needed to satisfy all the scheduling rules.
The Problem
Given an array of priorities where priorities[i] represents the priority level of the i-th process, return the minimum number of CPU cores you need to allocate.
Rules in Detail
- Each process must get at least 1 CPU core
- If a process has a higher priority than its left neighbor, it must get more cores than its left neighbor
- If a process has a higher priority than its right neighbor, it must get more cores than its right neighbor
- Processes with equal priorities can receive the same number of cores
Examples
Example 1
Input: [1, 0, 2] Output: 5 Explanation:
- Process 0 (priority 1): Gets 2 cores (higher than process 1)
- Process 1 (priority 0): Gets 1 core (minimum required)
- Process 2 (priority 2): Gets 2 cores (higher than process 1)
- Total: 2 + 1 + 2 = 5 cores
Example 2
Input: [1, 2, 2] Output: 4 Explanation:
- Process 0 (priority 1): Gets 1 core (minimum required)
- Process 1 (priority 2): Gets 2 cores (higher than process 0)
- Process 2 (priority 2): Gets 1 core (same priority as neighbor, but must be at least 1)
- Total: 1 + 2 + 1 = 4 cores
Example 3
Input: [1, 2, 3, 4, 5] Output: 15 Explanation:
- Each process gets cores in increasing order: 1, 2, 3, 4, 5
- Total: 1 + 2 + 3 + 4 + 5 = 15 cores
Implement the algorithm as described above.
cpp
#include <vector>
#include <algorithm>
int minimumCores(std::vector<int>& priorities) {
return 0;
}