Back to Fun Stuff

Apartments

Sort both arrays, then two-pointer sweep matching each applicant to the closest apartment within tolerance k.

Easy Sorting and Searching C++

Array Division

Binary search on the maximum subarray sum, greedily checking whether that limit needs at most k subarrays.

Easy Sorting and Searching C++

Building Roads

Solution implementation

Hard Graphs C++

Building Teams

Solution implementation

Hard Graphs C++

Collecting Numbers

Single pass over the positions array, counting how often the next number appears before the current one.

Easy Sorting and Searching C++

Collecting Numbers II

Maintain the round count from Collecting Numbers and update it incrementally after each swap by rechecking only the neighborhoods of the two swapped values.

Easy Sorting and Searching C++

Concert Tickets

Keep ticket prices in a multiset and use upper_bound to find the most expensive ticket within each customer’s budget.

Easy Sorting and Searching C++

Counting Rooms

Solution implementation

Hard Graphs C++

Course Schedule

Solution implementation

Hard Graphs C++

Dice Combinations

Solution implementation

Hard Dynamic Programming C++

Distinct Numbers

Insert every value into a map and read off the number of distinct keys.

Easy Sorting and Searching C++

Distinct Values Subarrays

Sliding window tracking the most recent duplicate position, counting subarrays ending at i with all distinct values.

Easy Sorting and Searching C++

Distinct Values Subarrays II

Sliding window capped at k distinct values, growing and shrinking the window while summing valid subarray lengths.

Easy Sorting and Searching C++

Distinct Values Subsequences

Count value frequencies, then multiply (count + 1) across all values and subtract 1 for the empty subsequence.

Easy Sorting and Searching C++

Dynamic Range Minimum Queries

Solution implementation

Medium Range Queries C++

Dynamic Range Sum Queries

Solution implementation

Medium Range Queries C++

Edit Distance

Solution implementation

Hard Dynamic Programming C++

Factory Machines

Binary search on time, checking whether all machines together can produce enough units by that time.

Easy Sorting and Searching C++

Ferris Wheel

Two-pointer greedy: try to pair the lightest remaining person with the heaviest, otherwise seat the heaviest alone.

Easy Sorting and Searching C++

Flight Discount

Solution implementation

Hard Graphs C++

Flight Routes

Solution implementation

Hard Graphs C++

Game Routes

Solution implementation

Hard Graphs C++

High Score

Solution implementation

Hard Graphs C++

Josephus Problem I

Simulate directly with an ordered set of survivors, walking k steps forward before each elimination.

Easy Sorting and Searching C++

Josephus Problem II

Binary search over a segment tree of alive positions to jump straight to the k-th surviving person after each elimination.

Easy Sorting and Searching C++

Longest Flight Route

Solution implementation

Hard Graphs C++

Maximum Subarray Sum

Kadane's algorithm: extend the running sum while it helps, and reset once it turns negative.

Easy Sorting and Searching C++

Maximum Subarray Sum II

Prefix sums plus a sliding multiset of window-start prefix sums to bound the subarray length between a and b.

Easy Sorting and Searching C++

Message Route

Solution implementation

Hard Graphs C++

Minimizing Coins

Solution implementation

Hard Dynamic Programming C++

Missing Coin Sum

Sort the coins and greedily extend the reachable prefix sum, stopping at the first gap.

Easy Sorting and Searching C++

Monsters

Solution implementation

Hard Graphs C++

Movie Festival

Greedy interval scheduling: sort by end time and take a movie whenever it starts after the last one ends.

Easy Sorting and Searching C++

Movie Festival II

Greedy interval scheduling generalized to k halls, tracked as a multiset of k free end times.

Easy Sorting and Searching C++

Nearest Smaller Values

Coordinate-compress the values and query a segment tree for the most recent earlier index holding a smaller value.

Easy Sorting and Searching C++

Nested Ranges Check

Sort ranges by (start, -end) and sweep once forward and once backward to mark containment.

Easy Sorting and Searching C++

Nested Ranges Count

Compress coordinates and sweep with a Fenwick-style segment tree to count containing and contained ranges.

Easy Sorting and Searching C++

Playlist

Sliding window keeping the last-seen position of every song, shrinking the window whenever a repeat appears.

Easy Sorting and Searching C++

Reading Books

Sum all reading times, but double the longest book’s time if it alone exceeds the rest combined (parallel reading).

Easy Sorting and Searching C++

Restaurant Customers

Sort arrivals and departures separately, binary searching departures to count customers still present at each arrival.

Easy Sorting and Searching C++

Room Allocation

Sweep arrival/departure events in time order, assigning the smallest currently free room number.

Easy Sorting and Searching C++

Round Trip

Solution implementation

Hard Graphs C++

Shortest Routes I

Solution implementation

Hard Graphs C++

Shortest Routes II

Solution implementation

Hard Graphs C++

Static Range Minimum Queries

Solution implementation

Medium Range Queries C++

Static Range Sum Queries

Solution implementation

Medium Range Queries C++

Stick Lengths

Sort the sticks and align every stick to the median, which minimizes total absolute distance.

Easy Sorting and Searching C++

Subarray Divisibility

Prefix sums modulo n, counting subarrays with remainder 0 via combinatorics on matching remainders.

Easy Sorting and Searching C++

Subarray Sums I

Prefix sums with a hashmap of counts to find subarrays summing to a fixed target.

Easy Sorting and Searching C++

Subarray Sums II

Same prefix-sum-and-hashmap approach as Subarray Sums I, extended to arrays with negative values.

Easy Sorting and Searching C++

Sum of Four Values

Fix the first two values, then two-pointer over the sorted remainder to complete the target sum.

Easy Sorting and Searching C++

Sum of Three Values

Fix the first value, then two-pointer over the sorted remainder to complete the target sum.

Easy Sorting and Searching C++

Sum of Two Values

Hashmap of value positions to find a complementary pair in one pass.

Easy Sorting and Searching C++

Tasks and Deadlines

Greedy: sort tasks by duration and process the shortest first to maximize total points from finish times.

Easy Sorting and Searching C++

Towers

Patience-sorting greedy: place each cube on the smallest existing tower it still fits on, else start a new tower.

Easy Sorting and Searching C++

Traffic Lights

Maintain a multiset of light positions and a parallel multiset of gap lengths, updating both as each new light is added.

Easy Sorting and Searching C++
#1068

Weird Algorithm

Simple simulation following the algorithm rules

Easy C++