Greedy

Greedy

Fifty-five questions on greedy algorithms, where the locally optimal choice at each step leads to a globally optimal solution.

Apple Redistribution into Boxes

easy
You are given an array apple of size n and an array capacity of size m.

Array Partition

easy
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized.

Assign Cookies

easy
Assume you are an awesome parent and want to give your children some cookies.

Buy Two Chocolates

easy
You are given an integer array prices representing the prices of various chocolates in a store.

Can Place Flowers

easy
You have a long flowerbed in which some of the plots are planted, and some are not.

DI String Match

easy
A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where: Given a string s, reconstruct the permutation perm...

Distribute Money to Maximum Children

easy
You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the...

K Items With the Maximum Sum

easy
There is a bag that consists of items, each item has a number 1, 0, or -1 written on it.

Largest Odd Number in String

easy
You are given a string num, representing a large integer.

Largest Perimeter Triangle

easy
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths.

Latest Time by Replacing Hidden Digits

easy
You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).

Lemonade Change

easy
At a lemonade stand, each lemonade costs $5.

Lexicographically Smallest Palindrome

easy
You are given a string s consisting of lowercase English letters, and you are allowed to perform operations on it.

Lexicographically Smallest String After a Swap

easy
Given a string s containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s with the same parity at most once.

Longest Palindrome

easy
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.

Longest Subsequence With Limited Sum

easy
You are given an integer array nums of length n, and an integer array queries of length m.

Longest Unequal Adjacent Groups Subsequence I

easy
You are given a string array words and a binary array groups both of length n.

Make Array Zero by Subtracting Equal Amounts

easy
You are given a non-negative integer array nums.

Maximize Sum Of Array After K Negations

easy
Given an integer array nums and an integer k, modify the array in the following way: You should apply this process exactly k times.

Maximum 69 Number

easy
You are given a positive integer num consisting only of digits 6 and 9.

Maximum Difference by Remapping a Digit

easy
You are given an integer num.

Maximum Odd Binary Number

easy
You are given a binary string s that contains at least one '1'.

Maximum Sum With Exactly K Elements

easy
You are given a 0-indexed integer array nums and an integer k.

Maximum Unique Subarray Sum After Deletion

easy
You are given an integer array nums.

Maximum Units on a Truck

easy
You are assigned to put some amount of boxes onto one truck.

Minimum Amount of Time to Fill Cups

easy
You have a water dispenser that can dispense cold, warm, and hot water.

Minimum Cost of Buying Candies With Discount

easy
A shop is selling candies at a discount.

Minimum Cost to Move Chips to The Same Position

easy
We have n chips, where the position of the ith chip is position[i].

Minimum Deletions for At Most K Distinct Characters

easy
You are given a string s consisting of lowercase English letters, and an integer k.

Minimum Hours of Training to Win a Competition

easy
You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.

Minimum Moves to Convert String

easy
You are given a string s consisting of n characters which are either 'X' or 'O'.

Minimum Number of Moves to Seat Everyone

easy
There are n availabe seats and n students standing in a room.

Minimum Number of Operations to Convert Time

easy
You are given two strings current and correct representing two 24-hour times.

Minimum Number of Pushes to Type Word I

easy
You are given a string word containing distinct lowercase English letters.

Minimum Operations to Make the Array Increasing

easy
You are given an integer array nums (0-indexed).

Minimum Subsequence in Non-Increasing Order

easy
Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.

Minimum Sum of Four Digit Number After Splitting Digits

easy
You are given a positive integer num consisting of exactly four digits.

Minimum Time to Type Word Using Special Typewriter

easy
There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer.

Partition Array Into Three Parts With Equal Sum

easy
Given an array of integers arr, return true if we can partition the array into three non-empty parts with equal sums.

Remove Digit From Number to Maximize Result

easy
You are given a string number representing a positive integer and a character digit.

Split a String in Balanced Strings

easy
Balanced strings are those that have an equal quantity of 'L' and 'R' characters.

Split With Minimum Sum

easy
Given a positive integer num, split it into two non-negative integers num1 and num2 such that: Return the minimum possible sum of num1 and num2.

Two Furthest Houses With Different Colors

easy
There are n houses evenly lined up on the street, and each house is beautifully painted.

Valid Palindrome II

easy
Given a string s, return true if the s can be palindrome after deleting at most one character from it.

Add Minimum Number of Rungs

medium
You are given a strictly increasing integer array rungs that represents the height of rungs on a ladder.

Advantage Shuffle

medium
You are given two integer arrays nums1 and nums2 both of the same length.

Append Characters to String to Make Subsequence

medium
You are given two strings s and t consisting of only lowercase English letters.

Append K Integers With Minimal Sum

medium
You are given an integer array nums and an integer k.

Apply Operations to Make Sum of Array Greater Than or Equal to k

medium
You are given a positive integer k.

Array of Doubled Pairs

medium
Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false...

Array With Elements Not Equal to Average of Neighbors

medium
You are given a 0-indexed array nums of distinct integers.

Avoid Flood in The City

medium
Your country has an infinite number of lakes.

Bag of Tokens

medium
You start with an initial power of power, an initial score of 0, and a bag of tokens given as an integer array tokens, where each tokens[i] denotes the value of tokeni.

Best Time to Buy and Sell Stock II

medium
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

Best Time to Buy and Sell Stock with Transaction Fee

medium
You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.