BEATS 96% 👏 || TWO-POINTER APPROACH || WELL EXPLAINED ✅
Intuition 🌄The objective is to count the number of boats that are required, based on the weight limit of the boats and the weight of people themselves.Here are
# Intuition 🌄
 The objective is to count the number of boats that are required, based on the weight limit of the boats and the weight of people themselves.

**Here are some key takeaways and tips:** 🌟
- Arrays/lists are easier to deal with when sorted.
- A common approach is to make a array that stores the amount of people in each boat, and reset it to an empty array when no one else can fit; however, this is ineffecient. 
- There is a very simple apprach that requires pointers that iterate from the start and end to the middle of the array.
- A max of two people can fit in a boat for some reason.

**Pointers are basically indexes of a list that iterates through it in a certain order, usually to keep track of elements. It is used in many algorithims.**

# Approach 🤔💭
1. Sort the array people. Why you ask?: It is easier to deal with in general and the two pointer approach will be more effecient this way.
2. Initialize 3 variables:
    - `low` will the the pointer that will iterate from the smallest weight to the middle (since we sorted people).
    - `high` will be the pointer that will iterate from the heaviest person to the middle.
    - `numOfBoats` will just keep track of how many boats are needed.
3. Iterate until the middle of the array, which means that all of the people have been accounted for.
4. In each iterations, first check if the heaviest and the lightest person can fit in the boat without exceeding the limit. If they can, then increment the lightest person to the next index of the array, because they have been accounted for.
5. Increment `high`, which stores the heaviest person's index, no matter what, because they will be able to fit in the boat along according the the problem's logic.
6. Increment the number of boats, because either two people or the heaviest person alone can fit into the boat, which have been accounted for.
7. Now, just return the variable you used to count the number of boats required.
# Complexity ⌚
- Time complexity:$$O(n * log(n))$$ 
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```java []
class Solution {
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int high = people.length-1; 
        int low = 0;
        int numOfBoats = 0;
        while(low <= high){
            if(people[low] + people[high] <= limit){
                low++;
            }
            high--;
            numOfBoats++;
        }
        return numOfBoats;
    }
}
```
```python []
class Solution(object):
    def numRescueBoats(self, people, limit):
        people.sort()
        boats = 0
        low = 0
        high = len(people)-1
        while(low<=high):
            if(people[low] + people[high] <= limit):
                low+=1
            high-=1
            boats += 1
        return boats
```
![image.png](https://assets.leetcode.com/users/images/a7921a74-7656-447e-b03a-fe39907000cf_1737682509.5868354.png)

['Two Pointers', 'Greedy', 'Python', 'Java', 'Python3']