Skip to content

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

Speccy-Rom
Copy link
Owner

@Speccy-Rom Speccy-Rom commented Jul 21, 2025

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:

  • Introduce Python implementation using a priority queue to handle alternating move and wait steps
  • Introduce Go implementation using a rolling DP array with a helper min function

Documentation:

  • Update Chinese and English READMEs to include the new Python and Go solutions

@Speccy-Rom Speccy-Rom self-assigned this Jul 21, 2025
Copy link

sourcery-ai bot commented Jul 21, 2025

Reviewer's Guide

Adds 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
    }
Loading

File-Level Changes

Change Details Files
Implement Python minCost solution with alternating move/wait logic
  • Introduced a heap-based Dijkstra over (i,j,time) states
  • Applied time parity to distinguish move versus wait steps
  • Tracked visited states with minimum costs to prune
  • Pushed initial state and handled early termination upon reaching target
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README.md
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README_EN.md
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/Solution.py
Implement Go minCost solution using 1D dynamic programming
  • Initialized a rolling dp array for the first row
  • Updated dp per row by taking min from above or left plus cell and enter costs
  • Added a min64 helper and final cost adjustment subtracting the last cell's cost
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README.md
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/README_EN.md
solution/3600-3699/3603.Minimum Cost Path with Alternating Directions II/Solution.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Speccy-Rom Speccy-Rom merged commit dc0b67e into main Jul 21, 2025
Copy link

@sourcery-ai sourcery-ai bot left a 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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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()
Copy link

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)

Suggested change
visited = dict()
visited = {}


ExplanationThe 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 [].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant