Blog

S1 Greedy Problems

Published 2 min read

1931 - Meeting Room Assignment

1931: Meeting Room Assignment

Say we pick which meetings to hold by choosing them 'one at a time' starting from the first meeting
You just pick the meeting that ends earliest.
Since we picked '1' meeting to start first, and picked the one that ends first,
the time available for meetings is maximized.[

1080 - Matrix

1080: Matrix

Let's consider it from (0, 0)
If A and B differ at (0, 0), there's only one possible case.
You always have to flip with (0,0) as the top-left of the 3x3 matrix.

Now look at (0, 1). (0, 0) is already the same in A and B, so
if A and B differ, you have to flip with (0, 1) as the top-left of the 3x3 matrix.

(0, 2) is the same now.
So if you consider (0, 0) first,
the number of cases for each move drops to 1,
so it has to be the answer.

1946 - New Recruits

1946: New Recruits

If an element is made up of several attributes and you have to sort several elements,
the most common approach is to pick one attribute, sort by it, and then think.
Also think about how the remaining attributes should be sorted.
(This problem only needs sorting by one attribute)

In this problem, each applicant has a document screening rank and an interview rank.
It doesn't matter which one you sort by, but let's solve it by sorting by document screening rank in ascending order.
In the order sorted by document screening rank in ascending order, look at the interview ranks.

No applicant can be worse than another applicant in both interview rank and document rank.
And since we look in the order sorted by document rank, an applicant we look at later has a worse document rank than one we looked at earlier.
So an applicant we look at later has to be better in interview rank than every applicant we've already seen.

Algorithmically, go through the applicants from the front while remembering the max interview rank,
and exclude from the answer any applicant smaller than the max.

15903 - Card Merging Game

15903: Card Merging Game

G4 Greedy Problems

It's the same problem as File Merging 3 in that link.

11497 - Log Jumping

11497: Log Jumping

In order of tallest log,
place each one at whichever of the left end or right end has the smaller height difference.

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub