Blog

Gold 5 Greedy Problem Patterns

Published 3 min read

11000 - Classroom Assignment

11000: Classroom Assignment

1374 - Classrooms

1374: Classrooms

19598 - Minimum Number of Meeting Rooms

19598: Minimum Number of Meeting Rooms

1689 - Overlapping Line Segments

1689: Overlapping Line Segments

If there's a class with Ti < Sj, the more you reuse classrooms, the fewer classrooms you need.

2212 - Sensors

2212: Sensors

13164 - Happy Kindergarten

13164: Happy Kindergarten

We want to cut out parts of a line segment so the sum of all remaining lengths is minimized.
Here, the longer the cut-out segments, the smaller the sum of the remaining lengths always gets.

12904 - A and B

12904: A and B

12931 - Adding Doubles

12931: Adding Doubles

From A there are 2 possible actions
but from B there's only 1 possible action
If you picture a graph, the problem situation becomes intuitive.

1092 - Ship

1092: Ship

Say there are cranes with weight limits 20 and 10, and boxes weighing 15 and 19,
if the 20 crane moves the 15 box, the 10 crane can't move the 19 box, so
it takes 2 minutes.

So starting from the heaviest crane, each one should pick and move the heaviest box it can carry.

1461- Library

1461: Library

This problem has two ideas in it.
Both ideas are greedy.

The first idea will make sense if you solve problem 11508 below first.
First split the books into positive and negative ones, then group them M at a time starting from the largest absolute values.

For the second idea, you have to count the steps going to and coming back for each group, so you multiply by two,
but for one of the groups you drop it off and don't have to come back, so you don't need to multiply it by two.
In other words you can take 1/2 of one group, and of course you should take 1/2 of the largest group to make the total number of steps smaller.

11508 - 2+1 Sale

11508: 2+1 Sale

2138 - Light Bulbs and Switches

2138: Light Bulbs and Switches

When it wraps around, split into CASEs based on one point and observe.

21758 - Collecting Honey

21758: Collecting Honey

if the honey is at the far left or far right
one of the bees always has to be at the opposite end from the honey to always eat the most honey.
For the other bee, consider every index and find the maximum. O(N)

else (the honey is at one of the positions other than the far left and far right)
the bees each have to be at both ends to eat the most honey
for the honey's position, consider every index to find it O(N)

19539 - Apple Trees

19539: Apple Trees

  1. The sum of all heights has to be a multiple of 3.

  2. If you sprayed 3 X times, then when you can spray 2 X times, it's obvious you can spray 1 X times.

19940 - Pizza Oven

19940: Pizza Oven

The boundary values matter. Think in terms of 35 and 5
Let's also try solving it with BFS

7983 - I'll Do It Tomorrow

7983: I'll Do It Tomorrow

6068 - Time Management

6068: Time Management

Think of it like taking an exam, the number of days you need to study and the end day are fixed, so
if you do it as late as possible you can play the most before you start studying.

16678 - Defilement

16678: Defilement

Find the minimum number of hackers needed to make a sequence whose natural-number common difference is 1 or less.

17828 - String Currency

17828: String Currency

The ASCII values of the letters should be small from the front.

23559 - Meals

23559: Meals

You always have to eat a meal, so at first assume you keep eating the 1000-won meal, which has no constraints.
Then, as far as the money allows, switch to the 5000-won meals that give the bigger gain in taste.

Comments

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

GitHub