Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 707 Bytes

91alg_day67_lc881.md

File metadata and controls

35 lines (30 loc) · 707 Bytes

Idea

  • sort the array, two pointers at two ends, move inward, try to put max&min together

Code

class Solution {
    public int numRescueBoats(int[] people, int limit) {
        if(people.length==1){
            return 1;
        }
        Arrays.sort(people);
        int res = 0;
        int low = 0;
        int high = people.length-1;
        while(low<=high){
            if(people[low]+people[high]<=limit){
                res++;
                low++;
                high--;
            }else{
                res++;
                high--;
            }
        }
        return res;
    }
}

Complexity Analysis

  • Time Complexity: O(nlogn)
  • Space Complexity: O(nlogn)