Brilliant Rescuer Beats 98% || Sorting || With Hints || Java, C++ ✅
Intuition\nThe question states that a boat can contain at most 2 persons.\nBear this statement in mind. Also, the max weight from the array will always be less 
# Intuition\n**The question states that a boat can contain at most 2 persons.**\nBear this statement in mind. Also, the max weight from the array will always be less or equal to the limit.\nSo, here you have your hint.\n# Hint:\n- You sort the people based on their weights.\n- You try to pair the currently lowest available weight person with the heaviest available weight person.\n- If their weight sum is less than or equal to the limit. Wala, we got a pair. Give these gentlemen/women a boat.\n- Then we move to the next available lowest and heaviest persons there is.\n- What if their weight sum exceeded the limit? Then, who should take the boat? Without thinking any further we should simply give the boat to the heaviest person. The lowest weight person has possibility to pair with another person. But the heaviest guy doesn\'t have. Because, as we move left to right, the weights increases.\n- So, as we are doing this comparisions, our boats count should be increased in every comparision.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nI\'ve just discussed the approach above! :-/\n\n![6810-pepeshhhh.png](https://assets.leetcode.com/users/images/0e5afc2e-cbf8-414f-a0a7-b1f2563d1089_1680550151.867256.png)\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N \\log N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int boats = 0;\n        int left = 0;\n        int right = people.length-1;\n        // Sort the peoples according to their weight in increasing order..\n        Arrays.sort(people);\n\n        // start comparing the lowest weight person(left) and the heaviest weight person(right)\n        while(left<=right){\n            int weight = people[left]+people[right];\n            // Either the weight is in limit or out of limit, we need a boat.\n            boats++;\n            // if the weight is in limit, these two people can have a boat.\n            // so we move on the the next persons of left and right\n            if(weight <= limit) {\n                left++;\n                right--;\n            }\n            else{\n                // if the weight is out of limit, the heaviest person gets the boat\n                // so, we decrement right.\n                right--;\n            }\n        }\n\n        return boats;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        int boats = 0;\n        int left = 0;\n        int right = people.size()-1;\n        // Sort the peoples according to their weight in increasing order..\n        sort(people.begin(), people.end());\n\n        // start comparing the lowest weight person(left) and the heaviest weight person(right)\n        while(left<=right){\n            int weight = people[left]+people[right];\n            // Either the weight is in limit or out of limit, we need a boat.\n            boats++;\n            // if the weight is in limit, these two people can have a boat.\n            // so we move on the the next persons of left and right\n            if(weight <= limit) {\n                left++;\n                right--;\n            }\n            else{\n                // if the weight is out of limit, the heaviest person gets the boat\n                // so, we decrement right.\n                right--;\n            }\n        }\n\n        return boats;\n    }\n};\n```\n\n![No Upvotes, Have a Good Day.png](https://assets.leetcode.com/users/images/dd35ce8e-4417-448c-a5f2-2a8628cf64da_1680550130.3513057.png)\n
['Sorting', 'C++', 'Java']