Tags

Greedy

Greedy

4 lessons
18 problems
2 question banks
9 community items

greedy

Algorithms

4 lessons

Approximation Algorithms

Advanced

50 min

2 prereqs

Vertex Cover is NP-hard: there is no known polynomial algorithm that finds a minimum cover. But a five-line greedy algorithm guarantees a cover whose size is at most twice the minimum, and the proof fits on a napkin. That guarantee, the approximation ratio, is the difference between a heuristic that might be terrible and an algorithm with a written contract about how far from optimal its answer can be. **Approximation Algorithms** introduces that contract-based view of NP-hard optimization. You will define approximation ratio formally and walk through the classic constant-factor algorithms: a 2-approximation for Vertex Cover via greedy maximal matching, an `O(log n)`-approximation for Set Cover via greedy element-counting, and a 2-approximation for Metric TSP that exploits the triangle inequality on top of an MST. The lesson then covers approximation schemes (PTAS, polynomial in `n` for any fixed `epsilon`; FPTAS, polynomial in both), with Knapsack FPTAS as the canonical example. Throughout, the lesson contrasts approximation algorithms with heuristics, where heuristics give no guarantee at all. In **Greedy (Intro)**, you saw simple greedy strategies and their correctness proofs. **Dynamic Programming (Advanced)** introduced 0/1 Knapsack as an NP-hard exact algorithm. This lesson asks the next question: when exact is too slow, how close to optimal can we provably get? Next, **Advanced Bit Manipulation** turns to bit-level tricks for `O(1)` and bitmask-DP solutions.

Not Started

0%

Algorithms
Approximation Algorithms
Greedy
Knapsack Problem
Advanced
Premium
Complexity Classes (P/NP)
Problem Solving

Branch and Bound

Advanced

50 min

2 prereqs

0/1 Knapsack is exponentially many subsets to consider, but commercial solvers like CPLEX and Gurobi answer instances with thousands of items in seconds. The trick is not better hardware but smarter exploration: at every node of the search tree, compute a cheap upper bound on what any completion of the partial solution could possibly achieve, and prune the entire subtree if that bound cannot beat the best solution found so far. That single discipline of provably-suboptimal pruning is what defines branch and bound. **Branch and Bound** introduces the framework as a refinement of backtracking. You will design bounding functions (typically a relaxation, like fractional knapsack as a bound for 0/1 knapsack), build state-space trees, and choose between three exploration strategies: FIFO (BFS-like), LIFO (DFS-like), and least-cost (priority queue). Classic applications include 0/1 Knapsack with the fractional-relaxation bound, the Traveling Salesman Problem, and the job-assignment problem. The lesson also draws the line between B&B and DP so you know when each is the right approach. In **Backtracking (Intro)**, you walked an implicit decision tree with choose, explore, unchoose. **Greedy (Intro)** taught you that locally optimal choices sometimes give global optima. Branch and bound borrows the search structure of one and the bounding intuition of the other. Next, **Divide and Conquer (Advanced)** turns to algebraic D&C tricks like Karatsuba and Strassen.

Not Started

0%

Algorithms
Branch and Bound
Backtracking
Greedy
Knapsack Problem
Advanced
Premium
Problem Solving

Advanced Greedy & Data Structures

Advanced

65 min

2 prereqs

"For each element in the array, find the next greater element" is a brute-force `O(n^2)` problem until you notice that a stack maintained in decreasing order lets you answer every query as a side effect of a single left-to-right scan. The whole algorithm runs in `O(n)`, and the same monotonic-stack pattern solves trapping rain water, largest rectangle in a histogram, stock spans, and a long tail of related problems with the same template. **Advanced Greedy & Data Structures** is where greedy thinking meets specialized scaffolding. You will implement the monotonic stack pattern for next-greater and next-smaller variants, the monotonic deque for sliding-window maximum and minimum (also a key DP optimization), and sweep-line algorithms for interval and event problems (meeting rooms, interval merge, rectangle area union). The lesson closes with advanced greedy problems that need a heap or priority queue: full Huffman encoding, task scheduling with cooldown, the gas station problem, and activity selection with deadlines and profits. In **Greedy (Intro)**, you saw simple greedy strategies that needed only sorting. **Heaps & Priority Queue** taught you `O(log n)` access to the minimum or maximum, which is exactly what these advanced greedy patterns rely on for their efficiency. Next, **Branch and Bound** extends backtracking with the same kind of bounding-function pruning, applied to optimization search trees.

Not Started

0%

Algorithms
Greedy
Monotonic Stack
Monotonic Queue
Sweep Line
Next Greater Element
Largest Rectangle in Histogram
Advanced
Premium

Greedy (Intro)

Intermediate

55 min

2 prereqs

Given coin denominations of 1, 5, and 25, the greedy "take the largest coin that fits" strategy gives change optimally. Add a coin worth 10 to the system and greedy stays optimal. Replace 25 with 21 and greedy quietly breaks: making 63 cents now wants three 21s, but greedy says one 21 plus two 1s and ten more 1s. The same algorithm, the same input shape, and yet correctness depends entirely on a structural property of the problem. **Greedy (Intro)** explains exactly which property that is. You will learn to test for the greedy-choice property and optimal substructure, the twin ingredients that justify a greedy decision. The lesson covers the canonical problems where greedy provably works (activity selection by earliest end time, fractional knapsack by value-to-weight ratio, job scheduling with deadlines, the jump game, Huffman encoding) and walks through the exchange-argument style of correctness proof. It also shows where greedy fails (0/1 knapsack, coin change with arbitrary denominations) so you know when to reach for DP instead. In **Sorting (Elementary)**, you saw why sorting often costs `O(n log n)`; almost every greedy algorithm starts with a sort step. **Big-O Notation (Upper Bound)** gave you the language to express that cost. Next, **Dynamic Programming (Intro)** picks up where greedy fails, when the local optimum does not guarantee the global one.

Not Started

0%

Algorithms
Greedy
Sorting
Interval Problems
Problem Solving
Intermediate
Premium

Practice Problems

18 problems

Candy

Not Started
Hard

Given an array of children's ratings, distribute the minimum number of candies such that each child gets at least one and children with higher ratings get more candies than their neighbors.

Greedy
Arrays
Algorithms
Advanced

684

10

Text Justification

Not Started
Hard

Given an array of words and a maximum width, format the text so that each line has exactly the specified number of characters, fully justified (left and right).

Strings
Simulation
Greedy
Advanced

798

12

Container With Most Water

Free
Not Started
Medium

Find two lines that together with the x-axis form a container holding the most water.

Two Pointers
Arrays
Greedy
Intermediate

628

15

Gas Station

Free
Not Started
Medium

Given arrays of gas available and cost to travel to the next station arranged in a circle, find the starting station index that lets you complete the circuit, or return -1 if impossible.

Greedy
Arrays
Circular Array
Algorithms
Intermediate

1.2k

33

Hand of Straights

Not Started
Medium

Given an array of integers and a group size, determine if the array can be rearranged into groups where each group consists of consecutive integers of the given size.

Greedy
Hash Map / Dictionary
Sorting
Arrays
Algorithms
Intermediate

405

2

Integer to Roman

Not Started
Medium

Convert an integer to its Roman numeral representation using a greedy approach with a value-symbol mapping.

Strings
Hash Map / Dictionary
Greedy
Intermediate

572

16

IPO

Free
Not Started
Medium

Maximize capital after completing at most k projects by greedily selecting the most profitable affordable projects using a max-heap.

Heap
Max Heap
Priority Queue
Greedy
Sorting
Intermediate

285

2

Jump Game II

Free
Not Started
Medium

Given an integer array where each element represents the maximum jump length at that position, find the minimum number of jumps needed to reach the last index.

Greedy
BFS
Arrays
Algorithms
Intermediate

948

26

Jump Game

Not Started
Medium

Given an integer array where each element represents the maximum jump length from that position, determine if you can reach the last index starting from the first index.

Dynamic Programming
Greedy
Arrays
Algorithms
Intermediate

610

7

Maximum Sum Circular Subarray

Not Started
Medium

Given a circular integer array, find the maximum possible sum of a non-empty subarray that may wrap around the ends of the array.

Greedy
Dynamic Programming
Kadane's Algorithm
Arrays
Algorithms
Intermediate

612

18

Meeting Rooms II

Not Started
Medium

Given an array of meeting time intervals, determine the minimum number of conference rooms required to hold all meetings.

Algorithms
Intermediate
Greedy
Sorting
Sweep Line
Heap
Interval Problems
Meeting Rooms
Premium

1k

7

Merge Triplets to Form Target

Not Started
Medium

Given a 2D array of triplets and a target triplet, determine if the target can be formed by choosing any subset of triplets and taking the element-wise maximum.

Greedy
Arrays
Algorithms
Intermediate

241

6

Minimum Arrows to Burst Balloons

Not Started
Medium

Given balloons represented as horizontal intervals, find the minimum number of vertical arrows needed to burst all balloons.

Algorithms
Intermediate
Greedy
Sorting
Interval Problems
Merge Intervals
Premium

238

3

Non-overlapping Intervals

Not Started
Medium

Given an array of intervals, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Algorithms
Intermediate
Greedy
Sorting
Interval Problems
Merge Intervals
Premium

371

7

Partition Labels

Not Started
Medium

Given a string, partition it into as many parts as possible so that each letter appears in at most one part, and return a list of the sizes of these parts.

Greedy
Strings
Hash Map / Dictionary
Algorithms
Intermediate

850

18

Best Time to Buy and Sell Stock II

Free
Not Started
Medium

Given an array of daily stock prices, find the maximum profit you can achieve with unlimited buy-sell transactions (one share at a time).

Greedy
Arrays
Algorithms
Intermediate

321

3

Task Scheduler

Free
Not Started
Medium

Determine the minimum number of intervals needed to execute all tasks given a cooldown period between identical tasks.

Heap
Max Heap
Priority Queue
Greedy
Frequency Count
Intermediate

745

11

Valid Parenthesis String

Not Started
Medium

Given a string containing '(', ')' and '*' characters, determine if the string can be valid by treating each '*' as either '(', ')', or an empty string.

Greedy
Dynamic Programming
Strings
Algorithms
Intermediate

564

9

Question Banks

2 items
Question Bank

Greedy Algorithms Warm-Up

Identify when a greedy choice is correct and when it fails. Drills cover activity selection, coin change with canonical denominations, and Huffman intuition.

Python
greedy
algorithms
quiz
fundamentals

728

13

Easy
Question Bank
Premium

JavaScript Binary Array Plant Pots: Two Approaches Quiz

Decide whether N new plants can be potted into a binary array of pots with no two plants adjacent. Two approaches (greedy single pass, lookahead with neighbour check), plus capacity counting and input validation.

JavaScript
quiz
arrays
greedy
interview-prep

350

8

Hard

Community

9 items
Problem
Medium
Free

Boats to Save People

Given person weights, a boat capacity, and a 2-person-per-boat limit, find the minimum number of boats.

greedy
two-pointers
sorting

179

4

May 13, 2026

by @chloesaeed

Problem
Medium
Free

Furthest Building You Can Reach

Use bricks for short climbs and ladders for tall ones, swapping greedily to maximize reach.

heap
priority-queue
greedy

470

2

4.3 (14)

Apr 20, 2026

by @davidtoure

Problem
Medium
Free

Two City Scheduling

Send exactly half the candidates to city A and half to city B at minimum total cost.

greedy
sorting
arrays

672

14

4.3 (12)

Apr 4, 2026

by @zarakamau

Problem
Hard
$9.99

Split Array Largest Sum

Split a non-negative integer array into k contiguous parts that minimize the maximum part sum, via binary search on the answer plus a greedy feasibility check.

binary-search
arrays
greedy

914

8

Apr 1, 2026

by @chidiweber

Problem
Medium
$6.99

Reorganize String

Rearrange a string so no two adjacent characters are equal, or report that no rearrangement exists.

heap
priority-queue
greedy
strings

385

9

4.3 (9)

Mar 27, 2026

by @oliviafoster

Problem
Medium
Free

Capacity to Ship Packages Within D Days

Binary-search-on-answer for the smallest belt capacity that ships all packages in order within D days, with a linear feasibility check per candidate.

binary-search
arrays
greedy

282

2

4.5 (12)

Jan 26, 2026

by @theowatanabe

Problem
Medium
Free

Min Add to Make Parentheses Valid

Count the minimum number of '(' and ')' insertions that turn a parenthesis string valid using a single-pass two-counter scan.

stack
greedy
strings

1.2k

27

4.6 (10)

Jan 3, 2026

by @kavyachakraborty

Article

Greedy vs DP: When Can You Actually Be Greedy?

The honest heuristic for proving a greedy choice is safe: exchange argument or matroid structure. Coin change, interval scheduling, and the activity-selection problem worked through.

greedy
dynamic-programming
algorithms
interval-problems
proof-techniques

357

6

4.4 (13)

Dec 29, 2025

by @carlosherrera

Problem
Hard
$8.99

Minimum Number of Refueling Stops

Reach the target with the fewest refueling stops, deferring fuel choices via a max-heap of passed stations.

heap
priority-queue
greedy

404

2

4.5 (11)

Nov 24, 2025

by @hanaokoro