JAVA Simple | Two Approaches | O(nlogn) with constant space | O(n) using count sort
\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int n = people.length;\n        \n       // Arrays.sort(people);         n
```\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int n = people.length;\n        \n       // Arrays.sort(people);         n log n method\n        \n        int count[] = new int[limit+1];\n        for(int p: people){\n            count[p]++;                        \n                                        //counting freq. of element \n        }\n        int i = 0;\n        for(int val = 1; val<=limit; val++){\n            while(count[val]-- > 0){\n                people[i++] = val; \n                                        //count sort ...sorts in O(n)\n            }\n        }\n        \n        int left = 0, right = n-1, boats = 0;       \n                                            // two pointers and will check in limit\n        while(left <= right){\n            if(people[left] + people[right] <= limit){\n                left++;\n                right--;\n            }\n            else{\n                right--;\n            }\n            boats++;\n        }\n        return boats;\n                            //        O(n log n) can be optimised to O(n) using Count Sort\n    }\n}       \n```
['Two Pointers', 'Sorting', 'Java']