Single Sequence Pairing¶
Table of Contents¶
- 2144. Minimum Cost of Buying Candies With Discount (Easy)
- 561. Array Partition (Easy)
- 1877. Minimize Maximum Pair Sum in Array (Medium)
- 881. Boats to Save People (Medium)
- 2592. Maximize Greatness of an Array (Medium)
- 2576. Find the Maximum Number of Marked Indices (Medium)
2144. Minimum Cost of Buying Candies With Discount¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
561. Array Partition¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting, counting sort
1877. Minimize Maximum Pair Sum in Array¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, greedy, sorting
881. Boats to Save People¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, greedy, sorting
881. Boats to Save People - Python Solution
from typing import List
# Left Right Pointers
def numRescueBoats(people: List[int], limit: int) -> int:
"""Returns the minimum number of boats to rescue people."""
people.sort()
left, right = 0, len(people) - 1
boats = 0
while left <= right:
if people[left] + people[right] <= limit:
left += 1
right -= 1
boats += 1
return boats
people = [3, 2, 2, 1]
limit = 3
print(numRescueBoats(people, limit)) # 3
2592. Maximize Greatness of an Array¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, greedy, sorting
2576. Find the Maximum Number of Marked Indices¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, binary search, greedy, sorting
2576. Find the Maximum Number of Marked Indices - Python Solution
from typing import List
# Fast Slow Pointers
def maxNumOfMarkedIndices(nums: List[int]) -> int:
nums.sort()
n = len(nums)
slow, fast = 0, n // 2
count = 0
while slow < n // 2 and fast < n:
if nums[fast] >= 2 * nums[slow]:
count += 2
slow += 1
fast += 1
return count
nums = [3, 5, 2, 4]
print(maxNumOfMarkedIndices(nums)) # 2