'''
解题思路:
双指针移动判断即可
'''
class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:

        P = people
        n = len(P)
        P = sorted(P)        
        print(P)

        i = 0
        j = n-1
        k = 0
        while i<=j:
            if i==j:
                if P[j]<=limit:
                    k += 1
                    j -= 1
            else:
                if P[i]+P[j]<=limit:
                    i += 1
                    j -= 1
                    k += 1
                elif P[j]<=limit:
                    j -= 1
                    k += 1
        return k


people = [3,2,2,1]
limit = 3           # 3
people = [3,5,3,4]
limit = 5           # 4
t = Solution().numRescueBoats(people,limit)
print(t)