LeetCode 2636: Promise Pool Solution
Master LeetCode problem 2636 (Promise Pool), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2636. Promise Pool
Problem Explanation
Explanation
To solve this problem, we can use a priority queue to keep track of the promises in the pool. We will process promises in ascending order of their deadline. At each iteration, we will check if we have enough resources to fulfill the promise. If so, we will fulfill the promise and move to the next one. If not, we will skip the promise and move to the next one. We will continue this process until all promises are either fulfilled or skipped.
Algorithm:
- Create a priority queue to store promises based on their deadlines.
- Iterate over the promises in ascending order of their deadlines.
- For each promise, check if we have enough resources to fulfill it.
- If we have enough resources, fulfill the promise and move to the next one.
- If we don't have enough resources, skip the promise and move to the next one.
- Repeat until all promises are either fulfilled or skipped.
Time Complexity
The time complexity of this algorithm is O(n log n) where n is the number of promises in the pool. This complexity is due to sorting the promises based on their deadlines and processing them in ascending order.
Space Complexity
The space complexity of this algorithm is O(n) where n is the number of promises in the pool. This is due to the priority queue used to store the promises.
Solution Code
import java.util.*;
class Promise {
int deadline;
int resources;
public Promise(int deadline, int resources) {
this.deadline = deadline;
this.resources = resources;
}
}
class Solution {
public int promisePool(List<Promise> promises, int totalResources) {
Collections.sort(promises, (a, b) -> a.deadline - b.deadline);
int fulfilledPromises = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (Promise promise : promises) {
if (promise.resources <= totalResources) {
totalResources -= promise.resources;
fulfilledPromises++;
} else {
pq.offer(promise.resources);
}
}
while (!pq.isEmpty() && pq.peek() <= totalResources) {
totalResources -= pq.poll();
fulfilledPromises++;
}
return fulfilledPromises;
}
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2636 (Promise Pool)?
This page provides optimized solutions for LeetCode problem 2636 (Promise Pool) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2636 (Promise Pool)?
The time complexity for LeetCode 2636 (Promise Pool) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2636 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2636 in Java, C++, or Python.