Greedy

Gas Station

LeetCode problem #134

Sum of Mutated Array Closest to Target

LeetCode problem #1300

Greedy

Master greedy algorithms by making locally optimal choices to find globally optimal solutions.

Buy and Sell Stock (Maximum Profit)

Find the maximum profit from buying and selling stock with at most one transaction

Candy

Distribute the minimum number of candies to children in a line such that each child gets at least one and children with higher ratings get more than their neighbors

Coin Change (Minimum Coins)

Find the minimum number of coins needed to make a given amount

Connect Ropes with Minimum Cost

Connect ropes with minimum cost using greedy approach with heap

Furthest Building You Can Reach

Find furthest building reachable using optimal allocation of bricks and ladders

Jump Game

Determine if you can reach the last index of an array where each element represents the maximum jump length from that position

Jump Game II

Given an array where each element represents the maximum jump length, find the minimum number of jumps to reach the last index

Maximize Sum After K Negations

Maximize array sum by flipping signs of k elements using greedy heap approach

Maximum Subarray Sum (Kadane's Algorithm)

Find the contiguous subarray with maximum sum using Kadane's algorithm

Meeting Rooms II

Find minimum number of meeting rooms required using heap

Merge Intervals

Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.

Minimum Insertions to Balance Parentheses

Find minimum insertions needed to balance parentheses string

Minimum Number of Platforms

Given arrival and departure times of trains at a station, find the minimum number of platforms required so that no train is kept waiting

Minimum Number of Refueling Stops

Find minimum refueling stops to reach target using greedy max-heap approach

Minimum Spanning Tree (MST): Kruskal vs Prim (DSU)

Learn what a minimum spanning tree is + MST algorithm steps. Kruskal (Union-Find) O(E log E) vs Prim (heap) O(E log V). Practice now.

Reduce Array Size to Half

Find minimum removals to reduce array size by half using greedy heap approach

Remove K Digits

Remove k digits from a number to make it as small as possible

Reorganize String (LC 767) — Greedy Max-Heap

Solve LC 767 with a greedy priority queue (max-heap). Learn the maxFreq ≤ ceil(n/2) condition + proof. O(n log k). Practice now.

Task Scheduler

Find the minimum number of intervals the CPU will take to finish all given tasks with a cooldown period between identical tasks

Task Scheduler

Schedule tasks with cooling period using greedy approach with heap