Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

This PR adds a solution to the Gas Station problem (LeetCode No. 134) to the greedy module.

Problem Statement

Given n gas stations along a circular route where:

  • Each station i has gas[i] amount of gas
  • It costs cost[i] gas to travel from station i to station i+1
  • Starting with an empty tank, find the starting station index that allows completing the full circuit
  • Return -1 if no solution exists

Algorithm

The solution uses a greedy approach with optimal time complexity:

  1. Check if total gas ≥ total cost (if not, return -1)
  2. Iterate through stations tracking cumulative net gain
  3. When net gain becomes negative, reset and try from the next station
  4. The last reset position is the valid starting point

Why this works: If we can't reach station i from station j, we also can't reach i from any station between j and i, so we can skip all intermediate stations.

Changes Made

  • ✅ Added src/greedy/gas_station.rs with complete implementation
  • ✅ Updated src/greedy/mod.rs to include the new module
  • ✅ Updated DIRECTORY.md with the new algorithm
  • ✅ Added comprehensive documentation with examples
  • ✅ Implemented 11 unit tests covering various scenarios

Testing

All tests pass successfully:

cargo test gas_station

Tests cover:

  • Basic functionality
  • Edge cases (single station, two stations)
  • No solution scenarios
  • Various starting positions
  • Large numbers
  • Boundary conditions

Complexity

  • Time Complexity: $O(n)$ - single pass through all stations
  • Space Complexity: $O(1)$ - only constant extra space

Code Quality

  • ✅ All tests passing
  • ✅ Formatted with cargo fmt
  • ✅ No clippy warnings (cargo clippy)
  • ✅ Comprehensive documentation
  • ✅ Examples in doc comments
  • ✅ Follows repository conventions

Type of Change

  • New algorithm implementation

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated DIRECTORY.md

References

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.03%. Comparing base (fb5784f) to head (5de9924).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1008      +/-   ##
==========================================
+ Coverage   96.02%   96.03%   +0.01%     
==========================================
  Files         377      378       +1     
  Lines       27217    27301      +84     
==========================================
+ Hits        26135    26219      +84     
  Misses       1082     1082              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@siriak
Copy link
Member

siriak commented Jan 27, 2026

We are not adding LeetCode problems

@siriak siriak closed this Jan 27, 2026
@AliAlimohammadi AliAlimohammadi deleted the add-gas-station-algorithm branch January 27, 2026 23:55
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.

3 participants