<p>There is a one-dimensional garden on the x-axis. The garden starts at the point <code>0</code> and ends at the point <code>n</code>. (i.e., the&nbsp;length of the garden is <code>n</code>).</p>

<p>There are <code>n + 1</code> taps located at points <code>[0, 1, ..., n]</code> in the garden.</p>

<p>Given an integer <code>n</code> and an integer array <code>ranges</code> of length <code>n + 1</code> where <code>ranges[i]</code> (0-indexed) means the <code>i-th</code> tap can water the area <code>[i - ranges[i], i + ranges[i]]</code> if it was open.</p>

<p>Return <em>the minimum number of taps</em> that should be open to water the whole garden, If the garden cannot be watered return <strong>-1</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png" style="width: 525px; height: 255px;" />
<pre>
<strong>Input:</strong> n = 5, ranges = [3,4,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, ranges = [0,0,0,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Even if you activate all the four taps you cannot water the whole garden.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>ranges.length == n + 1</code></li>
	<li><code>0 &lt;= ranges[i] &lt;= 100</code></li>
</ul>

// Structure to represent a pair of node and its distance
struct Pair {
    int node, distance;
    Pair(int _node, int _dist) : node(_node), distance(_dist) {};
    // Custom comparison operator for priority queue
    bool operator < (const Pair& pair) const {
        return node < pair.node;
    }
};

class Solution {
public:
    int minTaps(int n, vector<int>& ranges) {
        // Vector to store intervals (start, end)
        vector<pair<int, int>> intervals;

        // Create intervals from the given taps and ranges
        for (int i = 0; i <= n; ++i) {
            int start = max(0, i - ranges[i]);
            int end   = min(n, i + ranges[i]);
            intervals.emplace_back(start, end);
        }      

        // Sort intervals based on their start positions
        sort(intervals.begin(), intervals.end());

        // Vector to store the minimum distance to each node
        vector<int> minDistance(n + 1, INT_MAX);

        // Priority queue for Dijkstra's algorithm
        priority_queue<Pair> queue;
        queue.push(Pair(0, 0));

        // Dijkstra's algorithm
        while (!queue.empty()) {
            auto [node, distance] = queue.top();
            queue.pop();

            // Iterate through intervals to find reachable neighbors
            for (auto& [start, end] : intervals) {
                int nextDistance = distance + 1;

                // Skip intervals that are not relevant
                if (end <= node) 
                    continue;

                // Check if the current interval covers the next node
                if (start <= node && node <= end) {
                    // Relaxation step: update the distance 
                    // if a shorter path is found
                    if (nextDistance < minDistance[end]) {
                        minDistance[end] = nextDistance;
                        queue.push(Pair(end, nextDistance));
                    }
                }
            }
        }

        // Return the minimum distance to the last node, or -1 
        // if not reachable
        return (minDistance[n] != INT_MAX) ? minDistance[n] : -1;
    }
};
21223