1. 程式人生 > >885. Boats to Save People

885. Boats to Save People

885. Boats to Save People

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)

 

Example 1:

Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)

Example 2:

Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Note:

 

  • 1 <= people.length <= 50000
  • 1 <= people[i] <= limit <= 30000
  •  

利用two pointer的思想,首先我們需要一個記錄不重複體重的從小到大的陣列puniq;然後一個記錄相同體重人數的陣列dic

low = 0, high = n-1;詳見程式碼

class Solution {
public:
   int numRescueBoats(vector<int>& people, int limit) {
        int dic[50000 + 10], puniq[50000 + 10];
        memset(dic, 0, sizeof(dic));
        memset(puniq, 0, sizeof(puniq));
        int low = 0, high = 0;
        sort(people.begin(), people.end());
        for (int i = 0; i < people.size(); i++) {
            if (dic[people[i]] == 0) {
                dic[people[i]] = 1;
                puniq[high++] = people[i];
            }
            else {
                ++dic[people[i]];
            }
        }
        int boats = 0;
        high--;
        while (low < high) {
            if (puniq[low] + puniq[high] > limit) {
                boats += dic[puniq[high]];
                dic[puniq[high]] = 0;
                high--;
            }
            else {
                if (dic[puniq[low]] < dic[puniq[high]]) {
                    boats += dic[puniq[low]];
                    dic[puniq[high]] -= dic[puniq[low]];
                    dic[puniq[low]] = 0;
                    low++;
                }
                else if (dic[puniq[low]] > dic[puniq[high]]) {
                    boats += dic[puniq[high]];
                    dic[puniq[low]] -= dic[puniq[high]];
                    dic[puniq[high]] = 0;
                    high--;
                }
                else {
                    boats += dic[puniq[high]];
                    dic[puniq[low]] = dic[puniq[high]] = 0;
                    high--;
                    low++;
                }
            }
        }
        if (limit >= puniq[high]*2) {
            boats += ((dic[puniq[high]]/2) + (dic[puniq[high]]%2));
        }
        else {
            boats += dic[puniq[high]];
        }
        return boats;
    }


};

 

Python參見:http://www.cnblogs.com/seyjs/p/9428419.html

class Solution:
    def numRescueBoats(self, people, limit):
        """
        :type people: List[int]
        :type limit: int
        :rtype: int
        """
        dic = {}
        puniq = []
        for i in people:
            if i not in dic:
                dic[i] = 1
                puniq.append(i)
            else:
                dic[i] += 1
        puniq.sort()
        boats = 0
        ###利用two pointer的思想,low和high 進行體重的試探
        low = 0
        high = len(puniq) - 1
        while low < high:
            if (puniq[low] + puniq[high] > limit):
                boats += dic[puniq[high]]
                dic[puniq[high]] = 0
                high -= 1
            else:
                if dic[puniq[low]] > dic[puniq[high]]:
                    boats += dic[puniq[high]]
                    dic[puniq[low]] -= dic[puniq[high]]
                    dic[puniq[high]] = 0
                    high -= 1
                elif dic[puniq[low]] < dic[puniq[high]]:
                    boats += dic[puniq[low]]
                    dic[puniq[high]] -= dic[puniq[low]]
                    dic[puniq[low]] = 0
                    low += 1
                else:
                    boats += dic[puniq[low]]
                    dic[puniq[high]] = 0
                    dic[puniq[low]] = 0
                    low += 1
                    high -= 1

        if limit >= puniq[high]*2:
            boats += ((dic[puniq[high]]/2) + (dic[puniq[high]])%2)
        else:
            boats += dic[puniq[high]]
        return (int)(boats)