Skip to content

Comments

Implement solution for Best Time to Buy and Sell Stock III#7

Merged
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main
Oct 28, 2025
Merged

Implement solution for Best Time to Buy and Sell Stock III#7
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main

Conversation

@SjxSubham
Copy link
Contributor

Intuition

Each course may depend on others, forming a dependency graph. We start with courses having no prerequisites and “complete” them, freeing dependent courses. Repeating this, we process all courses whose prerequisites are satisfied. If all courses are completed, finishing is possible; otherwise, a cycle exists, making it impossible.

Approach

We first represent the course prerequisites as a directed graph, where each course is a node and an edge from course b to course a indicates that b must be completed before a. We then compute the in-degree for every course, which counts how many prerequisites each course has. Courses with zero in-degree can be taken immediately, so we add them to a queue. We repeatedly process courses from the queue, and for each course taken, we reduce the in-degree of its dependent courses. If a dependent course’s in-degree becomes zero, it means all its prerequisites are completed, and we add it to the queue. Finally, if we manage to process all courses, it means there are no cycles and it is possible to finish all courses; otherwise, the presence of a cycle prevents completion.

@Subhosjx Subhosjx self-assigned this Oct 28, 2025
@Subhosjx Subhosjx added the hacktoberfest-accepted hacktoberfest-accepted label Oct 28, 2025
@Subhosjx Subhosjx merged commit 5dc3544 into Subhosjx:main Oct 28, 2025
1 check passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @SjxSubham! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants