-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Isn't 121. Best Time to Buy and Sell Stock
a Greedy algorithm?
#152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey @Vandivier 👋🏽 We actually had this discussion recently over here: #129. It's been a very long time since I've solved this question but skimming over recent comments mention that this is a modified version of Kadane's algo. I'm totally open to adding |
Love your openness here. Let's start with a quick technical solution:
Greedy algorithms can be defined as any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. You can see that is what we are doing here. Depending on how we define DP, greedy algos are either a subset or an alternative approach. Let's define DP as any problem that applies a meta program to many subroutine solutions, then we can see that any greedy algorithm could be considered DP with a trivial meta program such as take latest, highest, smallest, etc from subroutine results. In this case, On the other hand, if we consider DP as specifically those problems where the local optima do not lead to global optima, eg the coin change problem, we can see DP as an exclusive pattern compared to greedy algos instead of a superset. |
Thank you for walking me through this - your logic was easy to follow at each step! I'll go ahead and reopen & merge #129 as to give credit to the original author and mark this as closed. Cheers! 😁 |
It's tagged as DP
I guess we can say "Greedy algos are a special case of DP" but I would still expect both tags to be on the problem
The text was updated successfully, but these errors were encountered: