[FEATURE] Addition of Weighted Interval Scheduling in C++ (#3014) #3035
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds an implementation of the Weighted Interval Scheduling problem under the Dynamic Programming section of the repository.
The problem involves selecting a subset of non-overlapping intervals, each with a start time, end time, and profit, such that the total profit is maximized.
This version extends the standard interval scheduling problem by introducing weights (profits), making it an excellent demonstration of dynamic programming with binary search optimization.
⸻
🚀 What’s Included
• 📘 Problem Statement: Clearly explained with examples
• ⚙️ Constraints & Assumptions: To make it realistic and well-scoped
• 🧮 Efficient Solution:
• Time Complexity: O(n log n) using DP + binary search
• Space Complexity: O(n)
• 💻 Language: C++
• 🧪 Test Cases: Added multiple test cases to ensure correctness
• 🧾 Documentation: Inline comments and/or README section for clarity
⸻
🧠 Key Concepts Demonstrated
• Interval sorting and binary search for compatibility
• Recurrence-based dynamic programming formulation
• Modular code structure with helper function for readability
• (Optional) Extension to reconstruct selected intervals
⸻
🎯 Why This Is a Valuable Addition
• A standard DP problem that’s foundational for many interview and competitive programming challenges.
• Currently missing from many algorithm repositories.
• Great for learners to understand optimization, binary search integration, and DP recurrence formulation.
• Encourages modular, educational code style aligned with the repo’s learning goals.
⸻
🧩 Implementation Notes
• Added a helper function findLastNonConflicting() to locate the last compatible interval efficiently using binary search.
• The main DP routine remains clean and intuitive.
• Code is modular, readable, and easily extendable.
⸻
✅ Checklist
• Code follows repository style and conventions
• Includes meaningful comments and documentation
• Tested with various input scenarios
• References issue #3014
⸻
🔗 Closes #3014