-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add minCost solution in Python and Go #3603 #2
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
Conversation
Reviewer's GuideAdds end-to-end Python and Go implementations of the minCost algorithm: the Python version uses a priority queue with time-parity states to alternate move and wait steps, while the Go version leverages a rolling 1D DP array to accumulate minimum path costs. Class diagram for the new Python Solution class (minCost)classDiagram
class Solution {
+minCost(m: int, n: int, waitCost: List[List[int]]) int
}
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Speccy-Rom - I've reviewed your changes - here's some feedback:
- The Python solution is missing imports (e.g. heapq and typing.List) needed to make it runnable as a stand-alone snippet.
- The Go DP version subtracts the final cell cost as a special case and doesn’t model the alternating wait/move steps the same way as the Python Dijkstra approach—consider aligning the algorithms or adding a time dimension to your DP.
- Unify naming conventions and high-level structure between the Python and Go implementations (e.g. consistent function signatures and variable names) to improve cross-language maintainability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The Python solution is missing imports (e.g. heapq and typing.List) needed to make it runnable as a stand-alone snippet.
- The Go DP version subtracts the final cell cost as a special case and doesn’t model the alternating wait/move steps the same way as the Python Dijkstra approach—consider aligning the algorithms or adding a time dimension to your DP.
- Unify naming conventions and high-level structure between the Python and Go implementations (e.g. consistent function signatures and variable names) to improve cross-language maintainability.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
class Solution: | ||
def minCost(self, m: int, n: int, waitCost: List[List[int]]) -> int: | ||
directions = [(1, 0), (0, 1)] # only down and right | ||
visited = dict() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Replace dict()
with {}
(dict-literal
)
visited = dict() | |
visited = {} |
Explanation
The most concise and Pythonic way to create a dictionary is to use the{}
notation.
This fits in with the way we create dictionaries with items, saving a bit of
mental energy that might be taken up with thinking about two different ways of
creating dicts.
x = {"first": "thing"}
Doing things this way has the added advantage of being a nice little performance
improvement.
Here are the timings before and after the change:
$ python3 -m timeit "x = dict()"
5000000 loops, best of 5: 69.8 nsec per loop
$ python3 -m timeit "x = {}"
20000000 loops, best of 5: 29.4 nsec per loop
Similar reasoning and performance results hold for replacing list()
with []
.
Summary by Sourcery
Add Python and Go solutions for the Minimum Cost Path with Alternating Directions II problem and update documentation to include them.
New Features:
Documentation: