Greedy from Smallest Largest¶
Table of Contents¶
- 3074. Apple Redistribution into Boxes (Easy)
- 2279. Maximum Bags With Full Capacity of Rocks (Medium)
- 1833. Maximum Ice Cream Bars (Medium)
- 1005. Maximize Sum Of Array After K Negations (Easy)
- 1481. Least Number of Unique Integers after K Removals (Medium)
- 1403. Minimum Subsequence in Non-Increasing Order (Easy)
- 3010. Divide an Array Into Subarrays With Minimum Cost I (Easy)
- 1338. Reduce Array Size to The Half (Medium)
- 1710. Maximum Units on a Truck (Easy)
- 3075. Maximize Happiness of Selected Children (Medium)
- 2554. Maximum Number of Integers to Choose From a Range I (Medium)
- 2126. Destroying Asteroids (Medium)
- 2587. Rearrange Array to Maximize Prefix Score (Medium)
- 976. Largest Perimeter Triangle (Easy)
- 1561. Maximum Number of Coins You Can Get (Medium)
- 3301. Maximize the Total Height of Unique Towers (Medium)
- 945. Minimum Increment to Make Array Unique (Medium)
- 1846. Maximum Element After Decreasing and Rearranging (Medium)
- 1647. Minimum Deletions to Make Character Frequencies Unique (Medium)
- 2971. Find Polygon With the Largest Perimeter (Medium)
- 2178. Maximum Split of Positive Even Integers (Medium)
- 2567. Minimum Score by Changing Two Elements (Medium)
- 1509. Minimum Difference Between Largest and Smallest Value in Three Moves (Medium)
- 3397. Maximum Number of Distinct Elements After Operations (Medium)
- 3457. Eat Pizzas! (Medium)
- 1262. Greatest Sum Divisible by Three (Medium)
- 948. Bag of Tokens (Medium)
- 1775. Equal Sum Arrays With Minimum Number of Operations (Medium)
- 2333. Minimum Sum of Squared Difference (Medium)
- 3440. Reschedule Meetings for Maximum Free Time II (Medium)
- 2141. Maximum Running Time of N Computers (Hard)
- 1196. How Many Apples Can You Put into the Basket (Easy) 👑
- 2214. Minimum Health to Beat Game (Medium) 👑
- 2098. Subsequence of Size K With the Largest Even Sum (Medium) 👑
- 2548. Maximum Price to Fill a Bag (Medium) 👑
- 3119. Maximum Number of Potholes That Can Be Fixed (Medium) 👑
- 2557. Maximum Number of Integers to Choose From a Range II (Medium) 👑
- 624. Maximum Distance in Arrays (Medium)
- 910. Smallest Range II (Medium)
- 2835. Minimum Operations to Form Subsequence With Target Sum (Hard)
- 3366. Minimum Array Sum (Medium)
3074. Apple Redistribution into Boxes¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
from typing import List
# Greedy
def minimumBoxes(apple: List[int], capacity: List[int]) -> int:
target = sum(apple)
capacity.sort(reverse=True)
res = 0
for box in capacity:
res += 1
target -= box
if target <= 0:
break
return res
apple = [1, 3, 2]
capacity = [4, 3, 1, 5, 2]
assert minimumBoxes(apple, capacity) == 2
#include <vector>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
class Solution
{
public:
int minimumBoxes(vector<int> &apple, vector<int> &capacity)
{
int s = accumulate(apple.begin(), apple.end(), 0);
sort(capacity.begin(), capacity.end(), greater<int>());
int i = 0;
while (s > 0)
{
s -= capacity[i++];
}
return i;
}
};
int main()
{
Solution s;
vector<int> apple = {1, 3, 2};
vector<int> capacity = {4, 3, 1, 5, 2};
cout << s.minimumBoxes(apple, capacity) << endl;
return 0;
}
2279. Maximum Bags With Full Capacity of Rocks¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
1833. Maximum Ice Cream Bars¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting, counting sort
1005. Maximize Sum Of Array After K Negations¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
- Return the maximum sum of the array after changing at most
k
elements.
from heapq import heapify, heapreplace
from typing import List
# Greedy
def largestSumAfterKNegationsGreedy(nums: List[int], k: int) -> int:
nums.sort(key=abs, reverse=True)
for i in range(len(nums)):
if nums[i] < 0 and k > 0:
nums[i] *= -1
k -= 1
if k % 2:
nums[-1] *= -1
return sum(nums)
# Heap
def largestSumAfterKNegationsHeap(nums: List[int], k: int) -> int:
heapify(nums)
while k and nums[0] < 0:
heapreplace(nums, -nums[0])
k -= 1
if k % 2:
heapreplace(nums, -nums[0])
return sum(nums)
print(largestSumAfterKNegationsGreedy([4, 2, 3], 1)) # 5
print(largestSumAfterKNegationsHeap([4, 2, 3], 1))
1481. Least Number of Unique Integers after K Removals¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, greedy, sorting, counting
1403. Minimum Subsequence in Non-Increasing Order¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
3010. Divide an Array Into Subarrays With Minimum Cost I¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, sorting, enumeration
1338. Reduce Array Size to The Half¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, greedy, sorting, heap priority queue
1710. Maximum Units on a Truck¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
3075. Maximize Happiness of Selected Children¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
from typing import List
# Greedy
def maximumHappinessSum(happiness: List[int], k: int) -> int:
selected = 0
happinessScore = 0
happiness.sort(reverse=True)
for score in happiness:
if selected == k:
return happinessScore
happinessScore += max(0, score - selected)
selected += 1
return happinessScore
happiness = [1, 2, 3]
k = 2
print(maximumHappinessSum(happiness, k)) # 4
2554. Maximum Number of Integers to Choose From a Range I¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, binary search, greedy, sorting
2126. Destroying Asteroids¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
2587. Rearrange Array to Maximize Prefix Score¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting, prefix sum
976. Largest Perimeter Triangle¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, math, greedy, sorting
1561. Maximum Number of Coins You Can Get¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, greedy, sorting, game theory
3301. Maximize the Total Height of Unique Towers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
945. Minimum Increment to Make Array Unique¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting, counting
from typing import List
# Greedy
def minIncrementForUnique(nums: List[int]) -> int:
nums.sort()
moves = 0
for i in range(1, len(nums)):
if nums[i] <= nums[i - 1]:
moves += nums[i - 1] + 1 - nums[i]
nums[i] = nums[i - 1] + 1
return moves
nums = [1, 2, 2]
print(minIncrementForUnique(nums)) # 1
1846. Maximum Element After Decreasing and Rearranging¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
1647. Minimum Deletions to Make Character Frequencies Unique¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: hash table, string, greedy, sorting
2971. Find Polygon With the Largest Perimeter¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting, prefix sum
2178. Maximum Split of Positive Even Integers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, backtracking, greedy
2567. Minimum Score by Changing Two Elements¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
1509. Minimum Difference Between Largest and Smallest Value in Three Moves¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
3397. Maximum Number of Distinct Elements After Operations¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
3457. Eat Pizzas!¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
1262. Greatest Sum Divisible by Three¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, greedy, sorting
948. Bag of Tokens¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, greedy, sorting
1775. Equal Sum Arrays With Minimum Number of Operations¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, greedy, counting
2333. Minimum Sum of Squared Difference¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, binary search, greedy, sorting, heap priority queue
3440. Reschedule Meetings for Maximum Free Time II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, enumeration
2141. Maximum Running Time of N Computers¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, binary search, greedy, sorting
1196. How Many Apples Can You Put into the Basket¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, greedy, sorting
2214. Minimum Health to Beat Game¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
2098. Subsequence of Size K With the Largest Even Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
2548. Maximum Price to Fill a Bag¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy, sorting
3119. Maximum Number of Potholes That Can Be Fixed¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, greedy, sorting
2557. Maximum Number of Integers to Choose From a Range II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, binary search, greedy, sorting
624. Maximum Distance in Arrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, greedy
from typing import List
# Array
def maxDistance(arrays: List[List[int]]) -> int:
mn, mx = float("inf"), float("-inf")
res = 0
for arr in arrays:
res = max(res, arr[-1] - mn, mx - arr[0])
mn = min(mn, arr[0])
mx = max(mx, arr[-1])
return res
arrays = [[1, 2, 3], [4, 5], [1, 2, 3]]
print(maxDistance(arrays)) # 4
910. Smallest Range II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, greedy, sorting
2835. Minimum Operations to Form Subsequence With Target Sum¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, greedy, bit manipulation
3366. Minimum Array Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming