0% found this document useful (0 votes)
2 views

Lesson24 Enumeration

Tony and Mary played a game with two-color pieces, where Tony needs to select a continuous section to flip in order to maximize the number of red pieces. The document outlines the problem, the operations involved, and an enumeration algorithm to find the maximum number of red pieces after a flip. It also discusses the algorithm's time complexity and potential optimizations for larger input sizes.

Uploaded by

snowsweater666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson24 Enumeration

Tony and Mary played a game with two-color pieces, where Tony needs to select a continuous section to flip in order to maximize the number of red pieces. The document outlines the problem, the operations involved, and an enumeration algorithm to find the maximum number of red pieces after a flip. It also discusses the algorithm's time complexity and potential optimizations for larger input sizes.

Uploaded by

snowsweater666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Enumeration

Question
Tony and Mary played a game using red and yellow two-color
pieces. Mary randomly arranged the two-color pieces in a
row, with some yellow sides up and some red sides up.
Tony needs to select a continuous section of two-color
pieces and flip each one, so that after one flip, the number
of red two-color pieces is maximized.
Can you help Tony figure out how many red two-color pieces
can be maximized in this game? (The number of two-color
pieces is less than 10.)
Question
Tony and Mary played a game using red and yellow two-color pieces. Mary
randomly arranged the two-color pieces in a row, with some yellow sides up and
some red sides up.
Tony needs to select a continuous section of two-color pieces and flip each one,
so that after one flip, the number of red two-color pieces is maximized. Can you
help Tony figure out how many red two-color pieces can be maximized in this
game? (The number of two-color pieces is less than 10.)
Example 1 Example 2

Flip Flip

2 red 4 red
Define the problem
n < 10
The state of a 2-color piece can be
Number of 2-color piece: <10 represented by the array colors of
Input Status of 2-color piece: Yellow length n
up or red up colors[i]: 0
1
Select a continuous strip of Select a start and an end (start
two-colored pieces <= end)
Operatio
n
Flip each piece in the selected Then invert all values in the range
strip (i.e., change red to yellow colors[start] to colors[end] (0 to
and yellow to red) 1,1 to 0)
By one flip operation, the Find a combination of start and end
Goal maximum number of two-color that flips the array colors with the
pieces with red up greatest number of 1s

The maximum number of red


Output Output the maximum
after the flip operation
Algorithm :
Enumeration
1. Iterate over all possible
combinations of start
and end .
2. For each group of start
and end, count the
number of ones in the
array colors after
flipping.
3. Record and update the
maximum number of 1.
4. Output the maximum
number of 1 as a result.
Algorithm :
Enumeration
initialize max_ones as 0
Iterate array colors start index from n-1 _______
0 _______to
Iterate array colors end index fromstart n-1
______ to _______
Iterate k in array colors from start to end
colors[k] =1-colors[k]
_____________________ # Filp colors[k]

set current_ones as the number of 1s

ifcurrent_ones
_____________ >max_ones
_____________ , then update max_ones

Iterate k in array colors from start to end


colors[k] = 1-colors[k]
_______________________ # Flip colors[k] back to its
original state
return max_ones
Code implementation

1. You may input different test data, record the


output, and verify with two-color pieces
Algorithm optimization

If there is no limit to the number of 2-color


piece, does the enumeration algorithm still
work?
The time complexity of this enumeration algorithm
is O(n^3) because there are three layers of nested
loops, each of which may be executed n times. For
small input sizes, this algorithm works, but for
larger input sizes, it becomes very inefficient. If the
problem grows in size, we should consider
Algorithm optimization
3
2-color
2.5
pieces Run time ( s )

Run time
(n) 2

1.5
5 0.0067108
1
50 0.0110654

s
0.5
100 0.0337362 0
5 50 100 500
500 2.673159
2-color pieces ( n )
Algorithm optimization

1.Orginal sufSum-
preSum interSum sufSum colors[end]
2.Orginal

colors 1 0 1 0 1 interSum+color
s[end]’s flip
3.Re-calculate the
start end
number of 1s in
array colors
Algorithm optimization
O(n3) O(n2) 3

2- 2.5
color Run time Run time

Run time
2
piece (s) (s)
s(n) 1.5
0.006710 0.001522 1
5
8 9

s
0.5
0.011065 0.002200
50
4 6 0
5 50 100 500
0.033736 0.003356
100
2 3 2-color pieces ( n )
0.046930
500 2.673159
5

You might also like