👏Beats 98.97% of users with Java🎉|| ✅Simple & Easy Well Explained Greedy Solution 🔥💥
Intuition\nThis problem aims to minimize the number of rescue boats required to save all people, pairing individuals if their combined weight permits within the
# Intuition\nThis problem aims to minimize the number of rescue boats required to save all people, pairing individuals if their combined weight permits within the given limit.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/cb70ba28-df4c-41de-9eac-e59ebbb5827f_1714794541.3869925.png)](https://leetcode.com/problems/boats-to-save-people/submissions/1248718581/?envType=daily-question&envId=2024-05-04)\n\n# Approach\n1. **Sort the array:** The function first sorts the array of people in ascending order. This is crucial for the algorithm to work efficiently.\n\n```java\nArrays.sort(people);\n```\n\n2. **Initialize pointers:** Two pointers, `i` and `j`, are initialized at the beginning and end of the sorted array, respectively.\n\n```java\nint i = 0, j = people.length - 1;\n```\n\n3. **Iterate through the array:** A while loop is used to iterate through the array from both ends (`i` from the beginning and `j` from the end) until they meet or cross each other.\n\n```java\nwhile (i <= j) {\n    // Code goes here...\n}\n```\n\n4. **Check if two people can fit in a boat:** Within the loop, it checks if the weight of the person at index `i` and the person at index `j` is less than or equal to the given limit.\n\n```java\nif ((people[j] + people[i]) <= limit) {\n    i++; // If they can fit, move to the next person from the lighter side.\n}\n```\n\n5. **Move pointer:** Regardless of whether two people can fit in a boat or not, the pointer `j` is decremented in each iteration to consider the next person from the heavier end of the array.\n\n```java\nj--;\n```\n\n6. **Count boats:** Increment the `boats` variable in each iteration to keep track of the number of boats required.\n\n```java\nboats++;\n```\n\n7. **Return the total number of boats:** Finally, the function returns the total number of boats required to rescue all the people.\n\n```java\nreturn boats;\n```\n\n# Complexity\n- Time complexity:\nO(n log n)\n\n- Space complexity:\nO(1)\n\n# Code\n```java\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int boats = 0;\n        Arrays.sort(people);\n        int i=0,j=people.length-1;\n        while(i<=j){\n            if((people[j]+people[i])<=limit){\n                i++;\n            }\n            j--;\n            boats++;\n        }\n        return boats;\n    }\n}\n```\n\n![_71eae110-0e71-4fc8-b986-1d9bcdd0a742.jpeg](https://assets.leetcode.com/users/images/de17805f-0d16-4824-8d97-c3ef6ee8016d_1714794574.4473996.jpeg)\n
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Java']