Skip to content

Conversation

@shubhambhar007
Copy link

This implementation tracks restaurant openings in a city grid and manages delivery zones using the Union-Find data structure for efficient connected component tracking.

Add Restaurant Delivery Zones Implementation with Union-Find
Summary
Implements an efficient city grid system for tracking restaurant openings and managing delivery zones using the Union-Find (Disjoint Set Union) data structure.

Features

  • Restaurant Management: Track multiple restaurants per city block with efficient opening operations
  • Delivery Zone Tracking: Automatically manage connected delivery zones using Union-Find algorithm
  • Path Compression: Optimized find operation with path compression for near-constant time complexity
  • Union by Rank: Efficient set merging to maintain balanced tree structure
  • Key Functionality
  • open_restaurant(r, c): Add restaurant at grid position with automatic zone merging
  • count_open_restaurants(): Get total count of opened restaurants
  • block_has_open_restaurants(r, c): Check if specific block has restaurants
  • count_delivery_zones(): Get number of connected delivery zones

Time Complexity

  • Initialize: O(rows × cols)
  • Open Restaurant: O(α(n)) amortized, where α is inverse Ackermann function (practically constant)
  • Query Operations: O(1) for counts and block checks
  • Space Complexity
  • O(rows × cols) for grid state and union-find structure

Demo & Testing
Includes comprehensive example usage demonstrating:

  • Restaurant opening and zone formation
  • Adjacent block merging
  • Multiple restaurants per block handling
  • Grid visualization
  • Real-time zone count tracking
  • Implementation Details
  • Uses iterative path compression for find operations
  • Implements union by rank for balanced merging
  • Maintains separate counters for total restaurants vs active blocks
  • Supports 4-directional connectivity (up, down, left, right)

This implementation tracks restaurant openings in a city grid and manages delivery zones using the Union-Find data structure for efficient connected component tracking.
Added detailed explanation of the union-find algorithm and refactored variable names for clarity.
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