Lesson25 Greedy
Lesson25 Greedy
Greedy Mission:
But
The cashier laughs, "What if the coins were
$0.50, $0.30, $0.05? Would your greedy
strategy still works?"
Greedy works when the
biggest/best choice doesn’t block
better future choices!
Amy's Weekend Adventure
It’s Saturday!
Amy has 4 hours
Activity List (with start/end times and "fun/skill score"):
to explore 9:00-10:30 AM: Pottery (7/10) – sculpting cute mugs.
10:00-11:30 AM: Coding (9/10) – build a robot!
activities. She 11:00 AM-12:30 PM: Dance (8/10) – learn K-pop moves.
1:00-2:30 PM: Painting (6/10) – watercolor flowers.
wants to 2:00-3:30 PM: Cooking (8/10) – bake cookies!
maximize
fun/skills learned.
Amy's Weekend Adventure
Final Schedule:
10-11:30 AM: Coding (9)
2-3:30 PM: Cooking (8)
The Pirate's Gold Dilemma
Your solution !
Section
Problem Description:
Given a sequence of N positive integers Ai, we
need to divide it into consecutive segments such
that the sum of each segment does not exceed M
(and can equal M). Determine the minimum Sample Input
number of segments required. 56
42451
Input Format:
The first line contains two integers N (length of the Sample Output
sequence) and M (maximum segment sum). The 3
second line lists N space-separated non-negative
integers Ai .
Output Format:
Print a single integer representing the minimum
number of segments.
Clumsy Cows S
Problem Description:
Bessie the cow is trying to type a balanced string of parentheses into her new laptop,
but she is sufficiently clumsy (due to her large hooves) that she keeps mis-typing
characters. Please help her by computing the minimum number of characters in the
string that one must reverse (e.g., changing a left parenthesis to a right parenthesis,
or vice versa) so that the string would become balanced.
There are several ways to define what it means for a string of parentheses to be
"balanced". Perhaps the simplest definition is that there must be the same total
number of ('s and )'s, and for any prefix of the string, there must be at least as many
('s as )'s. For example, the following strings are all balanced:
() (()) ()(()())
while these are not:
)( ())( ((())))
Sample
Given a sequence of parentheses of even length, ask the minimum Input
number of
())(
parentheses that can be modified to make it balanced.
Sample Output
Input Format: 2
1 line: A string of parentheses of even length at most 100,000 characters.
Output Format:
Poker Hands
Problem Description:
Bessie and her friends are playing a unique version of poker involving a deck
with N (1 <= N <= 100,000) different ranks, conveniently numbered 1..N (a
normal deck has N = 13). In this game, there is only one type of hand the
cows can play: one may choose a card labeled i and a card labeled j and play
one card of every value from i to j. This type of hand is called a "straight".
Bessie's hand currently holds a_i cards of rank i (0 <= a_i <= 100000). Help
her find the minimum number of hands she must play Sample
to getInput
rid of all her
cards. 5
2
4
Input Format:
1
Line 1: The integer N. 2
Lines 2..1+N: Line i+1 contains the value of a_i. 3
Problem Description:
We need to cut an N×M wooden board into 1×1 small squares.
For a wooden board, we can only cut along a horizontal or vertical line (along the grid
lines). Moreover, the board is not uniform, and cutting along different lines incurs
different costs. Additionally, after cutting a board once, it is divided into two pieces,
and we cannot reassemble these two pieces and cut them into four with a single cut.
Instead, we must cut each of the two pieces separately again.
Now, given the costs of cutting along different lines, find the minimum cost required
to divide the entire board into 1×1 small squares.
Input Format:
Line 1: contains N and M, representing the dimensions of the matrix (length
N and width M). Sample Input
Line 2: contains N−1 non-negative integers, representing 2the
2
costs of cutting along the N−1 horizontal lines. 3
3
Line 3: contains M−1 non-negative integers, representing the
costs of cutting along the M−1 vertical lines. Sample Output
9
Output Format: