Simple solution in Java
Approach\n Describe your approach to solving the problem. \nAs mentioned in the problem, each boat can have 2 persons at max whose total weight should be less t
# Approach\n<!-- Describe your approach to solving the problem. -->\nAs mentioned in the problem, each boat can have 2 persons at max whose total weight should be less than or equal to given limit. We need to find the minimum number of boats to carry each given person.\n\nLooking at the problem we can get the idea to use 2 pointers as we need to carry 2 persons at max. The weights of the persons is not given in the increasing/decreasing order to apply 2 pointers. So, for this we will do sorting on the people array.   \n\nNow take 2 pointers - one at 0(low) and other at n-1 (high) where n is the length of the people array. Take the weights of persons at low and high. If sum of the weights is <= given limit then those 2 persons can go in single boat. If sum of weights is > given limit then only one person can go in the boat and other person has to check with remaining persons. After this do the same for remaining persons. Finally return the boat count to the calling function.\n\n\n# Complexity\n- Time complexity:\n    For sorting - O(nlogn) \n    For iteration through loop - O(n)\n    Total time complexity - O(nlogn)\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```\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        // sorting the people array\n        Arrays.sort(people);\n        // res will have minimum number of boats required\n        int res=0,low=0,high=people.length-1,count=0;\n\n        while(low <= high){\n            count = people[low];\n            if(low != high)\n                count += people[high];\n            \n            // If the persons at low and high weighs <= given limit then use one boat for them and do the same for remaining persons\n            if(count <= limit){\n                res++;\n                low++;\n                high--;\n            }\n            // If the persons at low and high weighs > given limit then 2 persons cannot be sent in one boat. So sending one person in one boat and do the same for remaining persons.\n            else{\n                high--;\n                res++;\n            }\n        }\n        return res;\n    }\n}\n```
['Java']