Easy Java solution with an example of Parking spots (Easy to understand)
IntuitionIt was a struggle for me at first to understand this problem and the solutions people shared here .. I found an easy way of explaining thisApproachImag
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
It was a struggle for me at first to understand this problem and the solutions people shared here .. I found an easy way of explaining this


# Approach
<!-- Describe your approach to solving the problem. -->

*Imagine you're managing parking spaces along a street.*

**Analogy**:

**`blocks`**: *This is like a record of where parking spaces end. It's sorted by location (`index`).*
**`indexToLargestSize`**: *This is a record of the longest stretch of consecutive parking spaces ending at a particular location.*

**queries**: *These are requests:*
**Type 1: "Add a new parking space ending at location x."**
**Type 2: "Is it possible to park a car of size 'sz' ending at or before location x?"**

**Type 1 Query (Add a Parking Space):**

*When you add a new parking space ending at location `x`:*

*You check if there's a parking space ending just before `x` (using `blocks.lower(x)`).
If there isn't one, the longest stretch ending at `x` is simply `x` (meaning a stretch from the beginning of the street up to `x`).
If there is a parking space ending at lastBlock, the longest stretch ending at `x` is the longer of:
The distance between `x` and `lastBlock` (`x` - lastBlock).
The longest stretch ending at lastBlock (already recorded in `indexToLargestSize`).
Then, you check if adding this new parking space affects any subsequent recorded stretches. If the new stretch is larger than any of the existing ones, you update the indexToLargestSize.
Finally record the new ending of parking space in blocks.*

**Type 2 Query (Check if Parking is Possible):**

When someone asks if they can park a car of size `sz` ending at or before location `x`:

*You check if there's a parking space ending at or before `x` (using `blocks.floor(x)`).
If there isn't one, it means the street is empty up to `x`, so any car `size` (that fits within the length `x`) can park. You answer "yes".
If there is a parking space ending at `lastBlock`, you check two things:
Is the longest stretch ending at lastBlock big enough for the car `(indexToLargestSize.get(lastBlock) >= sz)?`
Is the distance between `x` and `lastBlock` big enough for the car `(x - lastBlock >= sz)?`
If either of these is true, the car can park. You answer "yes". Otherwise, you answer "no".*

# Complexity
- Time complexity: ***O(Nlogn)***

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public List<Boolean> getResults(int[][] queries) {
        int n = queries.length;
        Map<Integer, Integer> map = new HashMap<>();
        TreeSet<Integer> blocks = new TreeSet<>();
        List<Boolean> res = new ArrayList<>();

        for(int i = 0; i < n ; i++){
            int x = queries[i][1];
            if(queries[i][0] == 1){
                Integer lastBlock = blocks.lower(x);
                if(lastBlock == null)
                    map.put(x, x);
                else
                    map.put(x, Math.max(x - lastBlock, map.get(lastBlock)));

                int lastIndex = x;
                for(int index: blocks.tailSet(x)){
                    int prevSize = map.get(index);
                    int newSize = Math.max(index - lastIndex, map.get(lastIndex));
                    if(newSize == prevSize) break;
                    map.put(index, newSize);
                    lastIndex = index;
                }
                blocks.add(x);
            } else {
                int sz = queries[i][2];
                if(x < sz){
                    res.add(false);
                    continue;
                }
                Integer lastBlock = blocks.floor(x);
                if(lastBlock == null)
                    res.add(true);
                else {
                    if(map.get(lastBlock) >= sz || x - lastBlock >= sz)
                        res.add(true);
                    else
                        res.add(false);
                }
            }
        }
        return res;
    }
}
```
['Hash Table', 'Tree', 'Java']