Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 for is_pos, group in itertools.groupby(nums, key = bool): if is_pos: subgroup = list(group) num_negs = 0 # if the num negs is odd, the answer is either # the length without the first negative # or the length without the last negative first = last = None for i, n in enumerate(subgroup): if n < 0: num_negs += 1 if first is None: first = i last = i if num_negs % 2 == 0: res = max(res, len(subgroup)) else: res = max(res, first, len(subgroup) - 1 - first) res = max(res, last, len(subgroup) - 1 - last) return res
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) neg = [0] * n pos = [0] * n if nums[0] < 0: neg[0] = 1 elif nums[0] > 0: pos[0] = 1 # 以i为结尾的时候 乘积是负数的时候的长度 neg[i], or is positive for i in range(1, n): val = nums[i] if val > 0: pos[i] = pos[i-1] + 1 neg[i] = (neg[i-1] + 1) if neg[i-1] != 0 else 0 elif val < 0: pos[i] = (neg[i-1] + 1) if neg[i-1] != 0 else 0 neg[i] = pos[i-1] + 1 else: pos[i] = 0 neg[i] = 0 print((pos[i], neg[i])) return max(pos)
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: # space optimized def getMaxLen(self, nums: List[int]) -> int: ans = 0 neg_pos = None neg_count = 0 left = -1 for i, n in enumerate(nums): if n==0: neg_pos = None neg_count = 0 left = i continue elif n>0: if neg_count%2==0: ans = max(ans, i-left) else: ans = max(ans, i-neg_pos) elif n<0: neg_count += 1 if neg_pos is None: neg_pos=i if neg_count%2==0: ans = max(ans, i-left) else: ans = max(ans, i-neg_pos) return ans # original O(n) space def getMaxLen1(self, nums: List[int]) -> int: ans = 0 dq = [] left = -1 for i, n in enumerate(nums): if n==0: dq.clear() left = i continue elif n>0: if len(dq)%2==0: ans = max(ans, i-left) else: ans = max(ans, i-dq[0]) elif n<0: dq.append(i) if len(dq)%2==0: ans = max(ans, i-left) else: ans = max(ans, i-dq[0]) return ans
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: f_pos = 0 f_neg = 0 max_pos = 0 for i in range(len(nums)): if nums[i]==0: f_pos,f_neg = 0,0 elif nums[i]>0: f_pos +=1 if f_neg>0: f_neg+=1 else: tmp = f_pos if f_neg>0: f_pos = f_neg+1 else: f_pos = 0 f_neg = tmp+1 print((i, f_pos,f_neg)) max_pos = max(max_pos ,f_pos) return max_pos
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
from typing import List class Solution: def getMaxLen(self, nums: List[int]) -> int: def aux(i,j): negs = 0 for v in nums[i:j+1]: if v<0: negs+=1 if negs%2 == 0: return j-i+1 l=i while nums[l]>0: l+=1 l = j-l r=j while nums[r]>0: r-=1 r = r-i return max(l,r) start = 0 maxm = 0 for end in range(len(nums)): if nums[end] == 0: maxm = max(maxm,aux(start, end-1)) start = end+1 elif nums[end]>0: maxm = max(maxm, 1) if start!=len(nums)-1: maxm = max(maxm,aux(start, len(nums)-1)) return maxm
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
import bisect class Solution: ans = [0 for i in range(100001)] def getMaxLen(self, nums: List[int]) -> int: def solve(arr): s1, s2 = set(), set() s1.add(-1) ret = 0 cnt = 0 n = len(arr) for i, c in enumerate(arr): cnt += 0 if c > 0 else 1 if cnt % 2 == 0: s1.add(i) else: s2.add(i) if s1: ret = max(s1) - min(s1) if s2: ret = max(max(s2) - min(s2), ret) # print(s1, s2, ret) return ret ans = 0 l = 0 for i, c in enumerate(nums): if c == 0: ans = max(ans, solve(nums[l:i])) l = i + 1 ans = max(ans, solve(nums[l:])) return ans
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: def isSorted(arr, i, j): return all(arr[k] <= arr[k+1] for k in range(i, j)) ans = 0 ranges = [[0, len(A)-1]] for col in zip(*A): if not ranges: break if all(isSorted(col, i, j) for i, j in ranges): tmp = [] for i, j in ranges: start = i for k in range(i, j+1): if col[k] != col[start]: if k - start > 1: tmp.append([start, k-1]) start = k if j + 1 - start > 1: tmp.append([start, j]) start = k ranges[:] = tmp else: ans += 1 return ans
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: D = set() while True: changes = False last_str = '' for i, ch in enumerate(A[0]): if i not in D: last_str += ch for x in A[1:]: this_str = '' this_idx = [] for i, ch in enumerate(x): if i not in D: this_str += ch this_idx.append(i) while this_str < last_str: for i in range(len(this_str)): if this_str[i] < last_str[i]: D.add(this_idx[i]) this_idx = this_idx[:i] + this_idx[i+1:] this_str = this_str[:i] + this_str[i+1:] last_str = last_str[:i] + last_str[i+1:] changes = True break last_str = this_str if not changes: break return len(D)
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def checkLexOrder (self, A: List[str]): if A == sorted(A): return True else: return False def findDelIndices (self, A: List[str]): iter_ = len(A[0]) to_be_del = [] temp = ['']*len(A) for i in range(iter_): if self.checkLexOrder([x[i] for x in A]) == True: temp = [temp[j]+A[j][i] for j in range(len(A))] else: temp_ = [temp[j]+A[j][i] for j in range(len(A))] if self.checkLexOrder(temp_) == True: temp = [temp[j]+A[j][i] for j in range(len(A))] else: to_be_del.append(i) return len(to_be_del) def minDeletionSize(self, A: List[str]) -> int: if self.checkLexOrder(A): return 0 else: return self.findDelIndices(A)
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
import copy class Solution: def checkLexOrder (self, A: List[str]): if A == sorted(A): return True else: return False def findDelIndices (self, A: List[str]): iter_ = len(A[0]) to_be_del = [] temp = ['']*len(A) for i in range(iter_): if self.checkLexOrder([x[i] for x in A]) == True: temp = [temp[j]+A[j][i] for j in range(len(A))] else: temp_ = [temp[j]+A[j][i] for j in range(len(A))] if self.checkLexOrder(temp_) == True: temp = [temp[j]+A[j][i] for j in range(len(A))] else: to_be_del.append(i) return len(to_be_del) def minDeletionSize(self, A: List[str]) -> int: if self.checkLexOrder(A): return 0 else: return self.findDelIndices(A)
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def isArrSorted(self, a) -> tuple((bool, bool)): is_exp_sorted = True is_eq_sorted = True for i, l in enumerate(a): if i == len(a) - 1: break if not l < a[i+1]: is_exp_sorted = False if not l <= a[i+1]: is_eq_sorted = False return (is_exp_sorted, is_eq_sorted) def minDeletionSize(self, A: List[str]) -> int: deletion_count = 0 built_A = [''] * len(A) for i in range(0, len(A[0])): letters_at_i = [s[i] for s in A] is_exp_sorted, is_eq_sorted = self.isArrSorted(letters_at_i) # print(is_exp_sorted, is_eq_sorted) if is_exp_sorted: break test_A = [built_A[oi] + o[i] for oi, o in enumerate(A)] # print(test_A) if list(sorted(test_A)) == test_A: built_A = test_A continue else: deletion_count += 1 return deletion_count
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, words: List[str]) -> int: n = len( words ) if n==0: return 0 w = len( words[0] ) intervals = [[0,n]] del_cols = 0 for col in range(w): next_intervals = [] del_this_col = 0 for start,end in intervals: cprev = words[start][col] iprev = start for i in range(start+1,end): c = words[i][col] if c>cprev: ## create new interval ## reset cprev and iprev next_intervals.append( [iprev,i] ) cprev = c iprev = i elif c<cprev: del_this_col = 1 del_cols += 1 break pass if del_this_col: break ## add another last interval next_intervals.append( [iprev, end] ) pass if not del_this_col: intervals = next_intervals pass return del_cols
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A: List[str]) -> int: L = len(A) N = len(A[0]) sort_groups = [[i for i in range(L)]] count = 0 for i in range(N): new_groups = [] in_order = True for group in sort_groups: new_group = [] curr_ord = 0 for g_idx in group: char = A[g_idx][i] if ord(char) > curr_ord: if curr_ord != 0: new_groups.append(new_group) new_group = [g_idx] curr_ord = ord(char) elif ord(char) == curr_ord: new_group.append(g_idx) # remove a column else: in_order = False break if not in_order: break if new_group != []: new_groups.append(new_group) if in_order: sort_groups = new_groups else: count += 1 return count
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A): m, n = len(A), len(A[0]) first = [False] * m res = 0 for j in range(n): for i in range(1, m): if not first[i] and A[i][j] < A[i - 1][j]: res += 1 break else: for i in range(1, m): if A[i][j] > A[i - 1][j]: first[i] = True return res
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"]. Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]). Return the minimum possible value of D.length.   Example 1: Input: ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, A = ["a", "b", "c"]. Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]). We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1. Example 2: Input: ["xc","yb","za"] Output: 0 Explanation: A is already in lexicographic order, so we don't need to delete anything. Note that the rows of A are not necessarily in lexicographic order: ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...) Example 3: Input: ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.   Note: 1 <= A.length <= 100 1 <= A[i].length <= 100
class Solution: def minDeletionSize(self, A): m, n = len(A), len(A[0]) first = [-1] * m res = 0 for j in range(n): for i in range(1, m): if first[i] == -1 and A[i][j] < A[i - 1][j]: res += 1 break else: for i in range(1, m): if A[i][j] > A[i - 1][j] and first[i] == -1: first[i] = j return res
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.
class Solution: def findMaximumXOR(self, nums): """ :type nums: List[int] :rtype: int """ ans = 0 for bit in range(31, -1, -1) : ans = (ans << 1) + 1 pre = set() for n in nums : p = (n >> bit) & ans if p in pre : break pre.add(ans - p) else : ans -= 1 return ans
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.
class Solution: def findMaximumXOR(self, nums): """ :type nums: List[int] :rtype: int """ ans = 0 for bit in range(31, -1, -1) : ans = (ans << 1) | 1 pre = set() for n in nums : p = (n >> bit) & ans if p in pre : break pre.add(ans ^ p) else : ans ^= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo = 0 hi = len(people) - 1 count = 0 while lo <= hi: count += 1 if people[lo] + people[hi] <= limit: lo += 1 hi -= 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() boats = 0 l = 0 r = len(people) - 1 while r>l: boats += 1 if people[l] + people[r] <= limit: l += 1 r = r-1 else: r = r-1 if r == l: boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people, reverse=True) i = 0 j = len(people) - 1 n = 0 while True: # TODO - Check for [5,5,5,5,5] with limit of 5 if i >= j: break n += 1 w1 = people[i] i += 1 rem = limit - w1 if rem >= people[j]: j -= 1 if i == j: n += 1 return n
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() p1 = 0 p2 = len(people) - 1 boat = 0 while p1 <= p2: boat+= 1 if people[p1] + people[p2] <= limit: p1 += 1 p2 -= 1 return boat
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people)-1 count = 0 while i < j: if people[i] + people[j] <= limit: count += 1 i += 1 j -= 1 else: count += 1 j -= 1 if i == j: count += 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: new = sorted(people) i,j = 0, len(people)-1 res = 0 while i<=j: if new[j]+new[i]<=limit: i+=1 j-=1 res+=1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l, r = 0, len(people)-1 res = 0 while l <= r: while l <= r and people[l] + people[r] > limit: res += 1 r -= 1 if l <= r: res += 1 l += 1 r -= 1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: length = len(people) people.sort() left = 0 right = length - 1 boat_num = 0 while left <= right: if people[left] + people[right] <= limit: left += 1 right -= 1 else: right -= 1 boat_num += 1 return boat_num
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i=0 j=len(people)-1 count=0 while(i<=j): count+=1 if(people[i]+people[j]<=limit): i+=1 j-=1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if not people: return 0 people = sorted(people) left = 0 right = len(people) - 1 board_cnt = 0 while left <= right: #key if left == right: board_cnt += 1 break if people[left] + people[right] <= limit: left += 1 right -= 1 board_cnt += 1 else: right -= 1 board_cnt += 1 return board_cnt
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() left, right = 0, len(people)-1 boats = 0 while left <= right: if left == right: boats += 1 break if people[left] + people[right] <= limit: boats += 1 left += 1 right -= 1 else: right -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i,j=0,len(people)-1 ans=0 while i<=j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if len(people) <= 1: return 0 people.sort() boats = 0 l = 0 r = len(people) - 1 while l <= r: if l == r: return boats + 1 if people[l] + people[r] <= limit: boats += 1 l += 1 r -= 1 else: boats += 1 r -= 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) print(people) l = 0 r = len(people) - 1 boats = 0 while l <= r: if people[r] + people[l] <= limit: r -= 1 l += 1 boats += 1 else: r -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() r = len(people)-1 l = 0 trips = 0 while r >= l: if people[r] + people[l] <= limit: l += 1 trips += 1 r -= 1 return trips
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: # start at ends and move inward people.sort() start = 0 end = len(people) - 1 ans = 0 while start <= end: ans += 1 if people[start] + people[end] <= limit: start += 1 end -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) l, r = 0, len(people) - 1 while l <= r: if people[l] + people[r] <= limit: r -= 1 l += 1 return l
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) count = 0 left = 0 right = len(people) - 1 while left <= right: cur = people[left] + people[right] if cur <= limit: left += 1 right -= 1 elif cur > limit: right -= 1 count += 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: j = len(people)-1 i = 0 people = sorted(people) ### since we could max have two people on a boat so we need to add have two ###pointers print(people) count = 0 while(i<=j): count = count+1 if people[j] + people[i]<=limit: i = i + 1 j = j - 1 else: j = j - 1 return(count)
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: boats = 0 people = sorted(people) print(people) i, j = 0, len(people)-1 while i <= j: if people[i] + people[j] > limit: j -= 1 else: i += 1 j -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: s = [] c = 0 people.sort(reverse=True) for i in range(len(people)): if s and people[i] < s[-1]: s[-1] = s[-1] - people[i] s.pop() elif s and people[i] == s[-1]: s.pop() elif s and people[i] > s[-1]: s.append(limit - people[i]) c += 1 elif people[i] == limit: c += 1 else: c += 1 s.append(limit - people[i]) return c
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: less_than_half = dict() more_than_half = dict() half = 0 total_boats = 0 for person in people: diff = limit - person if diff == 0: total_boats += 1 continue if person < limit/2: if diff in more_than_half: total_boats += 1 if more_than_half[diff] == 1: del more_than_half[diff] else: more_than_half[diff] -= 1 else: if person in less_than_half: less_than_half[person] += 1 else: less_than_half[person] = 1 elif person > limit/2: if diff in less_than_half: total_boats += 1 if less_than_half[diff] == 1: del less_than_half[diff] else: less_than_half[diff] -= 1 else: if person in more_than_half: more_than_half[person] += 1 else: more_than_half[person] = 1 else: if half == 1: total_boats += 1 half = 0 else: half = 1 less_keys = sorted(less_than_half.keys()) more_keys = sorted(list(more_than_half.keys()), reverse=True) while len(less_keys) and len(more_keys): if less_keys[0] + more_keys[0] <= limit: if less_than_half[less_keys[0]] < more_than_half[more_keys[0]]: total_boats += less_than_half[less_keys[0]] more_than_half[more_keys[0]] -= less_than_half[less_keys[0]] less_keys.pop(0) elif less_than_half[less_keys[0]] > more_than_half[more_keys[0]]: total_boats += more_than_half[more_keys[0]] less_than_half[less_keys[0]] -= more_than_half[more_keys[0]] more_keys.pop(0) else: total_boats += less_than_half[less_keys[0]] less_keys.pop(0) more_keys.pop(0) else: total_boats += more_than_half[more_keys[0]] more_keys.pop(0) less_total = 0 for k in less_keys: less_total += less_than_half[k] more_total = 0 for k in more_keys: more_total += more_than_half[k] # we can pair up each of the less than half weights total_boats += (less_total+half + 1) // 2 + more_total return total_boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) first_index = 0 second_index = len(people) - 1 ans = 0 while first_index <= second_index: if people[first_index] + people[second_index] <= limit: second_index -= 1 first_index += 1 ans += 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) i = 0 j = len(people) - 1 total = 0 while i <= j: if i == j: total += 1 i += 1 j -= 1 else: if people[i] + people[j] <= limit: i += 1 j -= 1 total += 1 else: j -= 1 total += 1 return total # print(people) # while i < len(people): # print(i,people) # if i < len(people) - 1 and people[i] + people[i+1] <= limit: # print(\"two peeps\") # i += 2 # else: # print(\"one peep\") # i += 1 # total += 1 # return total
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
class Solution: def numRescueBoats(self, people: List[int], cap: int) -> int: people.sort() N = len(people) i, j = 0, N - 1 ans = 0 k = cap s = 2 while i <= j: # prioritize heavier one while s and j >= 0: if k - people[j] < 0: break k -= people[j] j -= 1 s -= 1 # check if we can fit any remainig lighter people while s and i < N: if k - people[i] < 0: break k -= people[i] i += 1 s -= 1 k = cap s = 2 ans += 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: sorted_people = list(sorted(people, reverse=True)) i = 0 j = len(people) - 1 while i <= j: if sorted_people[i] + sorted_people[j] <= limit: j -= 1 i += 1 return i
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() count = len(people) end = len(people) - 1 begin = 0 res = 0 while count > 0: if people[end] + people[begin] <= limit: res+=1 count-=2 end-=1 begin+=1 else: res+=1 count-=1 end-=1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: groups = self.group_people_by_weight(people, limit) return self.count_num_boats(groups, limit) def group_people_by_weight(self, people, limit): groups = [0] * (limit + 1) for person_weight in people: groups[person_weight] += 1 return groups def count_num_boats(self, groups, limit): num_boats = 0 start = 0 end = len(groups) - 1 while start <= end: while start <= end and groups[start] <= 0: start += 1 while start <= end and groups[end] <= 0: end -= 1 if start > end: break if start + end <= limit: groups[start] -= 1 groups[end] -= 1 num_boats += 1 return num_boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() low = 0 up = len(people) - 1 boats = 0 while(low <= up): if up-1 >= low and people[up] + people[up - 1] <= limit: up -= 2 boats += 1 elif up != low and people[up] + people[low] <= limit: up -= 1 low += 1 boats += 1 else: up -= 1 boats += 1 return boats
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
from collections import deque class Solution: def numRescueBoats(self, people, limit): n = len(people) weights = deque(sorted(people)) boat = 0 while n > 1: if weights[0] + weights[-1] <= limit: weights.popleft() weights.pop() n -= 2 else: weights.pop() n -= 1 boat += 1 if n == 1: boat += 1 return boat
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l = 0 h = len(people) -1 remain_capacity = limit if len(people) == 0: return 0 boat = 1 ppl_in_boat = 2 while ( l <= h ): if (people[h] <= remain_capacity and ppl_in_boat != 0): remain_capacity -= people[h] ppl_in_boat -= 1 h = h -1 elif (remain_capacity >= people[l] and ppl_in_boat != 0): remain_capacity -= people[l] ppl_in_boat -= 1 l = l +1 else: boat +=1 remain_capacity = limit ppl_in_boat = 2 return boat
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: num_boats = 0 counts = [0] * (limit + 1) for person_weight in people: counts[person_weight] += 1 start = 0 end = len(counts) - 1 while start <= end: while start <= end and counts[start] <= 0: start += 1 while start <= end and counts[end] <= 0: end -= 1 if start > end: break if start + end <= limit: counts[start] -= 1 counts[end] -= 1 num_boats += 1 return num_boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() print(people) low = 0 up = len(people) - 1 boats = 0 while(low <= up): if up-1 >= low and people[up] + people[up - 1] <= limit: up -= 2 boats += 1 elif up != low and people[up] + people[low] <= limit: up -= 1 low += 1 boats += 1 added = True else: up -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: n = len(people) people.sort() print(people) i = 0 j = n-1 re = [] c = 0 tmp = [] while i <= j: if people[i] + people[j] > limit: c +=1 tmp.append(people[j]) re.append(tmp) tmp = [] j = j-1 else: tmp.append(people[i]) tmp.append(people[j]) re.append(tmp) tmp = [] i = i+1 j = j-1 print(re) return len(re)
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() if people[0] >= limit: return 0 res = [0] i, j = 0, len(people) - 1 num_people = 0 while i <= j: if res[-1] + people[j] <= limit and num_people < 2: res[-1] += people[j] j -= 1 num_people += 1 elif res[-1] + people[i] <= limit and num_people < 2: res[-1] += people[i] i += 1 num_people += 1 else: res.append(0) num_people = 0 return len(res)
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i,j=0,len(people)-1 answer=0 while i<j: if people[i]+people[j]<=limit: i+=1 j-=1 else: j-=1 if i==j: answer+=1 answer+=1 return answer
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people) - 1 ans = 0 while i <= j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() num_boats=0 last=len(people)-1 first=0 while first < last: if people[first]+people[last]<=limit: last-=1 first+=1 num_boats+=1 else: num_boats+=1 last-=1 if first==last: num_boats+=1 return num_boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() left=0 right=len(people)-1 boats = 0 while left<=right: if left==right: boats+=1 break if people[left] + people[right] <= limit: left+=1 # right-=1 # boats+=1 # else: # right-=1 # boats+=1 right-=1 boats+=1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: l = len(people) if (l <= 1): return(1) # people = self.sortPeople(people) people.sort() i = 0; j = l-1; c = 0 while i < j: print((i,j,c)) if people[i] + people[j] <= limit: c +=1 i +=1 j -=1 else: j -=1 c +=1 if i == j: c +=1 return(c) def sortPeople(self, arr): # print(arr) l = len(arr) if l <= 1: return(arr) pivot = arr[l-1] larr = [];rarr = [] for i in range(l-1): if arr[i] >= pivot: rarr.append(arr[i]) else: larr.append(arr[i]) # print(larr + [pivot] + rarr) return(self.sortPeople(larr) + [pivot] + self.sortPeople(rarr))
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
class Solution(object): def numRescueBoats(self, people, limit): people.sort() i, j = 0, len(people) - 1 ans = 0 while i <= j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l = 0 r = len(people)-1 res = 0 while l <=r: if people[l]+people[r] <= limit: res+=1 l+=1 r-=1 else: res+=1 r-=1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i,j=0,len(people)-1 res=0 while i<=j: res+=1 if people[i]+people[j]<=limit: i+=1 j-=1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() if len(people) == 0 or len(people) == 1: return len(people) else: lptr = 0 rptr = len(people) - 1 count = 0 while lptr <= rptr: if( (people[lptr] + people[rptr]) <= limit): count += 1 lptr += 1 rptr -= 1 else: rptr -= 1 count += 1 return count ''' people.sort() left = 0 right = len(people) - 1 counter = 0 while left <= right: if people[left] + people[right] <= limit: left += 1 right -= 1 counter += 1 else: # people[left] + people[right] > limit: right -= 1 counter += 1 return counter'''
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i, j = 0, len(people) - 1 ans = 0 while i <= j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l, r = 0, len(people)-1 res = 0 while l <= r: res += 1 if people[l] + people[r] <= limit: l += 1 r -= 1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() left = 0 right = len(people) - 1 number = 0 while(left <= right): if (left == right): number +=1 break if people[left] + people[right] <= limit: left= left+1 right= right-1 number +=1 return number
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
from collections import deque class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = deque(sorted(people)) count = 0 while len(people) > 1: lightest = people.popleft() heaviest = people.pop() if lightest + heaviest <= limit: count += 1 continue else: if lightest < limit: people.appendleft(lightest) else: count += 1 count += 1 return count + len(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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: ''' time complexity : because of sorting O(Nlog(N)) the second loop is smaller so we ignore it space complexity: because of sorting O(N) ''' people.sort() left = 0 right = len(people)-1 boat_number = 0 while left <= right: if left == right: boat_number += 1 break if people[left] + people[right] <= limit: left += 1 right -= 1 boat_number+=1 else: right -= 1 boat_number +=1 return boat_number
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) res = 0 i = 0 j = len(people) - 1 while i <= j: if people[i] + people[j] <= limit: j -= 1 i += 1 return i
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo, hi = 0, len(people) -1 boats = 0 while lo <= hi: cap = limit cap -= people[hi] # always take the higher one first hi -= 1 if people[lo] <= cap: # if you can take a lower one to pair with higher, then include it lo += 1 boats += 1 # increment boat by 1, whether you can only take hi or lo + hi return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo = 0 count = 0 for hi in range(len(people))[::-1]: if lo < hi and people[lo] + people[hi] <= limit: lo += 1 if lo == hi: return len(people) - hi
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) i = 0 j = len(people)-1 res = 0 while i<=j: res+=1 if people[i]+people[j]<=limit: i+=1 j-=1 return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people)-1 boats = 0 while i <= j: if i == j or people[i]+people[j] <= limit: boats += 1 i += 1 j -= 1 else: boats += 1 j -= 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() n = len(people) i = 0 j = n-1 boats = 0 while i <= j: if people[i] + people[j] <= limit: i += 1 j -= 1 # else: # j -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) count = 0 i, j = 0, len(people) - 1 while 0 <= i < j < len(people): while 0 <= i < j < len(people) and people[i] + people[j] > limit: j -= 1 count += 1 i += 1 j -= 1 count += 1 if i == j: count += 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: minBoats = 0 people.sort() left = 0 right = len(people) - 1 while left <= right: if people[left] + people[right] <= limit: left += 1 minBoats += 1 right -= 1 return minBoats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: ret = 0 people.sort(reverse = True) start = len(people) - 1 for i in range(len(people)): if start >= i: ret += 1 temp_lim = limit - people[i] for j in range(start,i,-1): if people[j] <= temp_lim: start = j - 1 break elif people[j] > temp_lim: start = j break print(temp_lim) return ret
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i, j = 0, len(people)-1 boats = 0 while i <= j: if people[i] + people[j] <= limit: i += 1 j -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i=0 j=len(people)-1 m=0 while(i<=j): if people[i]+people[j]<=limit: i+=1 j-=1 else: j=j-1 m+=1 return m
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() n = len(people) i = 0 j = n-1 boats = 0 while i <= j: if i < j and people[i] + people[j] <= limit: i += 1 j -= 1 else: j -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: # Map: of weight to count O(n) # Check limit down people.sort() print(people) boats = 0 left_index = 0 right_index = len(people) - 1 while left_index <= right_index: if people[left_index] + people[right_index] <= limit: left_index += 1 boats += 1 right_index -= 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i=0 j=len(people)-1 boats=0 while(i<=j): if people[i]+people[j]<=limit: i+=1 j-=1 elif people[i]+people[j]>limit: j-=1 boats+=1 return boats
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
# 1, 2, 2, 3 limit = 4 # l r class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) left = 0 right = len(people) - 1 counter = 0 while left < right: total = people[left] + people[right] counter += 1 right -= 1 if total <= limit: left += 1 if left == right: counter += 1 return counter
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: total = 0 #if len(people)%2 == 0 else 1 people.sort() print(people) i = 0 j = len(people) - 1 while i <= j: print(j) if (people[i] + people[j]) > limit or i == j: j-=1 total+=1 else: i+=1 j-=1 total+=1 return total
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) res = 0 print(people) l, r = 0, len(people)-1 while(l<=r): print((l, r)) remain = limit-people[l] res += 1 l += 1 if remain - people[r] >= 0: remain -= people[r] r -= 1 print(res) return res
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: # start=0 # end=len(people)-1 # count=0 # #people=sorted(people) # while start<=end: # if people[start]+people[end]<=limit: # start+=1 # end-=1 # count+=1 bucket = [0]*(limit+1) for i in people: bucket[i]+=1 start = 0 end = len(bucket)-1 count=0 while start<=end: while start<=end and bucket[start]<=0: start+=1 while start<=end and bucket[end]<=0: end-=1 if bucket[start]<=0 and bucket[end]<=0: break count+=1 if start+end<=limit: bucket[start]-=1 bucket[end]-=1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if people == None or len(people) == 0: return 0 people.sort() start = 0 end = len(people)-1 counter = 0 while start <= end: if people[start] + people[end] <=limit: start += 1 end -=1 else: end -= 1 counter += 1 return counter
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i,j = 0, len(people) -1 ans = 0 while i<=j: ans+=1 if people[i]+people[j] <= limit: i+=1 j-=1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() left, right = 0, len(people) - 1 count = 0 while left <= right: if people[left] + people[right] <= limit: left += 1 right -= 1 count += 1 else: right -= 1 count += 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i, j = 0, len(people) -1 count = 0 while i <= j: if i == j: count += 1 break if people[i] + people[j] <= limit: i+=1 j-=1 else: j-=1 count += 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: minBoats = 0 people.sort() left = 0 right = len(people) - 1 while left <= right: if people[left] + people[right] <= limit: minBoats += 1 left += 1 right -= 1 elif people[left] + people[right] > limit: minBoats += 1 right -= 1 return minBoats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people)-1 total = 0 while i <= j: total += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return total
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people) - 1 count = 0 while(i <= j): if people[i] + people[j] > limit: count = count + 1 j = j - 1 else: count = count + 1 i = i + 1 j = j - 1 return(count)
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: q = collections.deque(sorted(people)) ans = 0 while len(q) > 1: i,x = q.pop(), q[0] if i+x <= limit: q.popleft() ans += 1 return ans + (1 if q else 0)
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i = 0 j = len(people) - 1 ans = 0 while i <= j: if i == j: return ans + 1 elif people[i] + people[j] > limit: j -= 1 ans += 1 else: i += 1 j -= 1 ans += 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: total = 0 #if len(people)%2 == 0 else 1 people.sort() i = 0 j = len(people) - 1 while i <= j: if (people[i] + people[j]) > limit or i == j: j-=1 total+=1 else: i+=1 j-=1 total+=1 return total
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i,j = 0, len(people)-1 count = 0 while i <= j: if people[i] + people[j] <= limit: count += 1 i += 1 j -= 1 else: count += 1 j -= 1 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) res = 0 print(people) l, r = 0, len(people)-1 while(l<=r): print((l, r)) remain = limit-people[l] res += 1 l += 1 if remain - people[r] >= 0: remain -= people[r] r -= 1 print(res) return res ''' '''
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
import heapq class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() i, j = 0,len(people)-1 ans = 0 while i <= j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() p1 = 0 p2 = len(people) -1 count = 1 cur = 0 curNum = 0 while p1 <= p2: if people[p2] <= limit - cur and curNum < 2: cur = cur + people[p2] curNum += 1 p2 -=1 continue if people[p1] <= limit - cur and curNum < 2: cur = cur + people[p1] curNum += 1 p1 += 1 continue count += 1 cur = 0 curNum = 0 return count
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: # sort 1 - sort in the ascending order of weight people.sort() boats = 0 i = 0 j = len(people) - 1 while j>=i: # check if two people can sit in a boat if people[i] + people[j] <= limit: i += 1 j -= 1 boats += 1 return boats
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
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people, reverse=True) boats = 0 left = 0 right = len(people) - 1 while left <= right: if people[left] + people[right] <= limit: right -= 1 left += 1 boats += 1 return boats